2012-02-13 10 views
0

私は長い間この疑問を抱いていますが、PHPのリファレンスをどのように扱うのが良いアイデアなのですか?例を使用するよりも説明できないので、次のクラスを見てから@ setResultメソッドのコメントPHPのメモリ参照

モデルビューコントローラフレームワークを使用していて、基本的なAjaxControllerを構築しているとしましょう。これまでのところ、1つのアクションメソッド(getUsers)しかありませんでした。コメントを読んで、私の質問がはっきりしていることを願っています.PHPはこのような状況をどのように処理するのですか?setResult docblockのx回について書きました。

class AjaxController{ 
    private $json = array(
     'result' => array(), 
     'errors' => array(), 
     'debug' => array() 
    ); 

    /** 
    * Adds an error, always displayed to users if any errors. 
    * 
    * @param type $description 
    */ 
    private function addError($description){ 
     $this->json['errors'][] = $description; 
    } 

    /** 
    * Adds an debug message, these are displayed only with DEBUG_MODE. 
    * 
    * @param type $description 
    */ 
    private function addDebug($description){ 
     $this->json['debug'][] = $description; 
    } 

    /** 
    * QUESTION: How does this go in memory? Cause if I use no references, 
    * the array would be 3 times in the memory, if the array is big (5000+) 
    * its pretty much a waste of resources. 
    * 
    * 1st time in memory @ model result. 
    * 2th time in memory @ setResult ($resultSet variable) 
    * 3th time in memory @ $this->json 
    * 
    * @param array $resultSet 
    */ 
    private function setResult($resultSet){ 
     $this->json['result'] = $resultSet; 
    } 

    /** 
    * Gets all the users 
    */ 
    public function _getUsers(){ 
     $users = new Users(); 
     $this->setResult($users->getUsers()); 
    } 

    public function __construct(){ 
     if(!DEBUG_MODE && count($this->json['debug']) > 0){ 
      unset($this->json['debug']); 
     } 

     if(count($this->json['errors']) > 0){ 
      unset($this->json['errors']); 
     } 

     echo json_encode($this->json); 
    } 
} 

別の簡単な例:技術Aを使用する方がよいだろうか。

function example(){ 
    $latestRequest = $_SESSION['abc']['test']['abc']; 

    if($latestRequest === null){ 
     $_SESSION['abc']['test']['abc'] = 'test'; 
    } 
} 

または技法B:

function example(){ 
    $latestRequest =& $_SESSION['abc']['test']['abc']; 

    if($latestRequest === null){ 
     $latestRequest = 'test'; 
    } 
} 

読んでくれてありがとうと助言:)

+1

参考文献に関するPHPマニュアルページ:[http://php.net/manual/en/language.references.php](http://php.net/manual/en/language.references.php) – bfavaretto

答えて

2

で短い:参照を使用しないでください。

書き込み時のPHPコピー。検討してください:

$foo = "a large string"; 
$bar = $foo; // no copy 
$zed = $foo; // no copy 
$bar .= 'test'; // $foo is duplicated at this point. 
       // $zed and $foo still point to the same string 

参照機能は、機能が必要な場合にのみ使用してください。つまり、元の配列またはスカラーの参照を介してそれを変更する必要があります。

+0

ありがとうあなたの答え、私は今それを理解する。私はちょうど別の例を追加しました。あなたがそれを見て、どのテクニックを使うのが良いかアドバイスできればすばらしいでしょう。もう一度、ありがとう:) – randomKek

+0

@MikeVercoelen、元のデータを更新するためにその機能が必要なので、参照されたものを設定する2番目の例は参照の適切な使用方法です。それは何か目立つ方法で物事をスピードアップするつもりはないので、それのためにそれを使用しないでください。また、参照が終わったら、あとで誤って再利用しないように 'unset()'する必要があります。そして、その例が考案されたことは分かりますが、一般的には、このような複雑なネストされた配列を完全に避けることがより良い解決策です。通常、それはオブジェクトがより適切であることを意味し、これはrefの必要性を軽減します。 – Matthew

+0

このような複雑な配列が適切な状況にいる場合は、コピー/貼り付けのエラーを排除するため、例のように参照を使用する傾向があります。しかし、答えのこの部分は、基本的に人々が意見に同意できない意見です。 – Matthew