1

Possible Duplicate:
Reference assignment operator in php =&

i want to create a blog,this is the controller file but i do not understand this line

$this->$model =& new $model; what do this line?

    protected $_model;
    protected $_controller;
    protected $_action;
    protected $_template;

     function __construct($model, $controller, $action) {

        $this->_controller = $controller;
    $this->_action = $action;
    $this->_model = $model;

    $this->$model =& new $model;
    $this->_template =& new Template($controller,$action);

}

function set($name,$value) {
    $this->_template->set($name,$value);
}

function __destruct() {
        $this->_template->render();
}

}
Community
  • 1
  • 1
mirhossein
  • 682
  • 7
  • 16
  • I don't think this question should be closed as a duplicate. There are other components to the question than just the `&`. `>$`, `new $` are of interest. – Explosion Pills Jul 30 '12 at 14:29
  • Since PHP 5.x it is not recommended to to use `=&`, when dealing with objects. Any code you see using it will signify that it is still using old PHP4.x mindset. It cause memory leak due to messing with reference count. Also, FYI, model in MVC is not a class, but a layer. – tereško Jul 30 '12 at 15:09

2 Answers2

2

The $model variable is interpolated. Say it says "Welcome." This is interpolated to

$this->Welcome = & new Welcome;

The & does nothing in PHP 5 and should be removed. In PHP 4 it was necessary for the member to maintain a reference to the object instance.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • thanks for response but i know that but what it means here,the model is object so what mean $this->$model means?(with 2 dollar sign),is this creating new object from model class??? – mirhossein Jul 30 '12 at 14:30
  • I explained that above. Please reread the first couple paragraphs a few times. – Explosion Pills Jul 30 '12 at 14:32
  • thanks,what book do you offer me for oophp that be simple and intermediate? – mirhossein Jul 30 '12 at 14:34
  • Just a side note: you might want to edit that code `$this->$model` to `$this->{$model}`. It'll work fine as is, but the php docs do advise against it. By using `$this->{}`, you can concatenate strings, use arrays and function return values, too: `$this->{'some'.$this->getString().$model}`. I use this trick to simulate method overloading (type hint to abstract object, and then use the return value of `get_class($argument)` to call private member functions etc... – Elias Van Ootegem Jul 30 '12 at 14:39
  • thanks elias,i'll keep it in mind – mirhossein Jul 30 '12 at 14:44
  • -1, the `=&`, when dealing with objects, does quite a lot. And all of it bad. – tereško Jul 30 '12 at 15:33
  • 1
    @tereško that's not really fair .. I said it should be removed at least. – Explosion Pills Jul 30 '12 at 17:14
0

This line means that its developer didn't know how memory in PHP5(__destruct is only in PHP5) works.

Dmitry
  • 7,457
  • 12
  • 57
  • 83