【问题标题】:what means $this->$object =& new $class in php [duplicate]php中的$this->$object =& new $class是什么意思[重复]
【发布时间】:2012-07-28 05:46:10
【问题描述】:

可能重复:
Reference assignment operator in php =&

我想创建一个博客,这是控制器文件但我不明白这一行

$this->$model =& new $model; 这条线做什么?

    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();
}

}

【问题讨论】:

  • 我认为这个问题不应该重复关闭。除了&,这个问题还有其他组成部分。 >$, new $ 很感兴趣。
  • 从 PHP 5.x 开始,不建议在处理对象时使用=&。您看到的任何使用它的代码都表明它仍在使用旧的 PHP4.x 思维方式。由于弄乱了引用计数,它会导致内存泄漏。另外,仅供参考,MVC 中的模型不是一个类,而是一个层。

标签: php oop


【解决方案1】:

$model 变量被插值。说“欢迎”。这是内插到

$this->Welcome = & new Welcome;

& 在 PHP 5 中没有任何作用,应该被删除。在 PHP 4 中,成员必须维护对对象实例的引用。

【讨论】:

  • 感谢您的回复,但我知道这意味着什么,模型是对象,那么 $this->$model 是什么意思?(带有 2 美元符号),这是从模型创建新对象吗上课???
  • 我在上面解释过。请重新阅读前几段。
  • 谢谢,你为我提供了什么简单和中级的 oophp 书?
  • 附注:您可能希望将该代码 $this->$model 编辑为 $this->{$model}。它会按原样正常工作,但 php 文档确实建议不要这样做。通过使用$this->{},您也可以连接字符串、使用数组和函数返回值:$this->{'some'.$this->getString().$model}。我用这个技巧来模拟方法重载(类型提示抽象对象,然后使用get_class($argument)的返回值调用私有成员函数等......
  • @tereško 这不太公平。我说至少应该删除它。
【解决方案2】:

这一行意味着它的开发者不知道 PHP5 中的内存(__destruct 仅在 PHP5 中)是如何工作的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 2011-05-06
    • 2015-12-03
    • 2013-06-15
    • 2012-05-04
    相关资源
    最近更新 更多