我有一些模块化(对象和类)JavaScript 和 PHP。我不断得到摆脱全局变量的建议......“它们很糟糕”......我猜是因为它们会导致依赖关系。
但是我应该把环境变量放在哪里——最好的例子是路径信息。
这是一个典型的例子。
下面的 UploadFile 类需要
const PICTURES = '../pictures/';
特别是在这个地方(无需阅读整个课程)
$this->path_medium = UploadedFile::PICTURES . "$this->sessionId.jpg";
$this->path_small = UploadedFile::PICTURES . "$this->sessionId-1.jpg";
到没有上传文件的地方。我宁愿把它放在 GlobalClass 中并从那里访问它。这是有道理的,因为其他代码需要知道图片的去向,而我只有一个位置可以进行更新。
是否拥有可以作为全局变量的环境变量,以便可以从多个模块访问它们,但只能在一个地方进行编辑。
<?php
/**
* Module : Model
* Name : UploadFile
* Input : File Information
* Output : Resized Files in .jpg format
* Notes :
resizeMove() - resizes the picture to $maxMedium and $maxSmall and moves the file to a permanent location.
makeDimensions() - calculates the dimensions of the new images so that there is not distortion if possible.
getImage() - creates a php image for manipulation.
updateSessionAndDb - updates the mysql table - move out.
*/
class UploadedFile
{
const PICTURES = '../pictures/';
private $originalWidth,
$originalHeight,
$newWidth,
$newHeight,
$maxMedium = 50,
$maxSmall = 20;
private $src = NULL;
private
$fileType,
$fileName,
$sessionId,
$path_medium,
$path_small;
function __construct($fileType, $fileName)
{
$this->sessionId = Session::get('id');
$this->path_medium = UploadedFile::PICTURES . "$this->sessionId.jpg";
$this->path_small = UploadedFile::PICTURES . "$this->sessionId-1.jpg";
$this->fileType = $fileType;
$this->fileName = $fileName;
}
public function createImages()
{
if(move_uploaded_file($this->fileName, $this->path_medium))
{
if($this->getImage($this->path_medium))
{
list($this->originalWidth,$this->originalHeight)=getimagesize($this->path_medium);
$this->resizeMove($this->maxMedium,$this->path_medium);
$this->resizeMove($this->maxSmall,$this->path_small);
imagedestroy($this->src);
}
}
}
private function resizeMove($max, $path)
{
$this->makeDimensions($max);
$image_true_color = imagecreatetruecolor($this->newWidth, $this->newHeight);
imagecopyresampled($image_true_color, $this->src, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->
originalWidth, $this->originalHeight);
imagejpeg($image_true_color, $path);
imagedestroy($image_true_color);
}
private function makeDimensions($max)
{
$this->newWidth=$this->originalWidth;
$this->newHeight=$this->originalHeight;
if(($this->originalWidth > $this->originalHeight) && ($this->originalWidth > $max))
{
$this->newWidth = $max;
$this->newHeight = ($max / $this->originalWidth) * $this->originalHeight;
}
elseif($this->originalHeight > $this->originalWidth && $this->originalHeight > $max)
{
$this->newHeight = $max;
$this->newWidth = ($max / $this->originalHeight) * $this->originalWidth;
}
elseif ($this->originalWidth > $max)
{
$this->newWidth = $this->newHeight = $max;
}
}
private function getImage($path)
{
$type_creators = array(
'image/gif' => 'imagecreatefromgif',
'image/pjpeg' => 'imagecreatefromjpeg',
'image/jpeg' => 'imagecreatefromjpeg',
'image/png' => 'imagecreatefrompng');
if(array_key_exists($this->fileType, $type_creators))
{
$this->src = $type_creators[$this->fileType]($path);
return true;
}
return false;
}
}
对象制作器示例
class ObjectMaker
{
public function makeSignUp()
{
$DatabaseObject = new Database();
$TextObject = new Text();
$MessageObject = new Message();
$SignUpObject = new ControlSignUp();
$SignUpObject->setObjects($DatabaseObject, $TextObject, $MessageObject);
return $SignUpObject;
}
对象生成器示例已修改
class ObjectMaker
{
public function makeSignUp()
{
$DatabaseObject = new Database();
$TextObject = new Text();
$MessageObject = new Message();
return new ControlSignUp( $DatabaseObject, $TextObject, $MessageObject );
}
笔记:
我有可能类似于 SignUp 的类,因此我可以使用继承来通过使用 ObjectMaker 类来减少对象创建的冗余 - 回到原来的问题 - 我可以在这个“模式”中包括注入全局变量而不仅仅是对象。