2

いくつかのモジュラー (オブジェクトとクラス) JavaScript と PHP があります。私はグローバル変数を取り除くようにアドバイスを受け続けています...「それらは悪いです」...それらは依存関係を引き起こすためだと思います。

ただし、環境変数はどこに配置すればよいでしょうか? 最良の例はパス情報です。

これが典型的な例です。

以下の UploadFile クラスが必要です

const     PICTURES = '../pictures/';

特にこの場所では(クラス全体を読む必要はありません)

$this->path_medium = UploadedFile::PICTURES . "$this->sessionId.jpg";
$this->path_small = UploadedFile::PICTURES . "$this->sessionId-1.jpg";

ファイルをアップロードする場所がありません。私はそれを GlobalClass に入れて、そこからアクセスしたいと思います。他のコードは画像の移動先を知る必要があり、更新を行う場所は 1 つしかないため、これは理にかなっています。

環境変数をグローバルとして使用しても問題ないため、複数のモジュールからアクセスできますが、1 か所でしか編集できません。

<?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 クラスを使用してオブジェクト作成の冗長性を減らすことができます - 元の問題に戻ります - この「パターン」にオブジェクトだけでなくグローバルの注入を含めることができます。

4

3 に答える 3

3

クラスを再利用可能にしたい場合は、そこに何かをハードコードするのではなく、代わりに注入する必要があります。イメージのセッション ID とベース ディレクトリのように。それらをパラメーターとしてコンストラクターに追加します。

function __construct($fileType, $fileName, $sessionId, $prefixPictures)
{
    $this->fileType = $fileType;
    $this->fileName = $fileName;
    $this->sessionId = $sessionId;
    $this->prefixPictures = $prefixPictures;

    $this->path_medium = $prefixPictures . $this->sessionId . ".jpg";
    $this->path_small = $prefixPictures . $this->sessionId . "-1.jpg";
}

さらに、独自のクラスを作成する必要があります。1 つは画像のサイズを変更するためのもので、もう 1 つはサイズの計算を行うためのものです。実際にはそのようなクラスは既に存在するため、ファイルをインクルードしてサイズ変更を行うだけです (例: WideImageを使用)。

于 2012-04-16T19:15:28.833 に答える
1

おそらくそうではありません。パスクラスのプロパティを出力するパスゲッターを作成できます(つまりPaths::pictures

繰り返しになりますが、そのレベルでは、開発者次第です。

組織のために、それらをすべて名前空間に貼り付けて、そのように呼び出すのが良いと思います。新しい開発者が登場したときに役立ちます

于 2012-04-16T19:03:13.863 に答える
1

定数はグローバル変数ほど悪くはなく、パスに定数を使用することは、「真の」ルート パス変数を持たない PHP の不便さを克服する最も一般的な方法です (現在のスクリプトに基づいてDIRを使用することはできますが、現在のスクリプトを呼び出したスクリプトに基づく)。

グローバルが良くない主な理由は、グローバルだからではなく、スクリプトのどこでも変更できるからです。実際、これは PHP に関する私の主な不満の 1 つです。$_SERVER 変数はスクリプトの実行中に変更される可能性があり、それらが定数であってほしいと思います。

定数は定義して使用することしかできず、編集することはできません。グローバルの存在を十分に文書化する限り、それらを管理することは、他の PHP 環境変数を管理することと同じくらい難しくありません。

あなたが彼らに夢中にならない限り、それは問題ありません。テストに関しては、環境が常に役割を果たすことを忘れないでください。環境を認識していて、簡単にモックできる限り、テストにこれ以上の問題は発生しません。

于 2012-04-16T19:03:36.817 に答える