【问题标题】:Deprecated: Assigning the return value of new by reference is deprecated on empty lines of code不推荐使用:在空代码行上不推荐使用通过引用分配 new 的返回值
【发布时间】:2013-05-24 14:38:52
【问题描述】:


我知道这个问题已经回答了很多次了,但是我有一些我无法解决的问题。

我收到了几个关于在特定页面和特定行上分配返回值的警告。但是当我打开那个页面时,请求的行要么是空的,要么是在 cmets 下,要么是其他的.. 这是一个警告和与之相关的页面代码:

不推荐使用:通过引用分配 new 的返回值是 已弃用 /home/saltushr/public_html/_classes/hr/dimedia/framework/Controller.class.php 第 25 行

不推荐使用:通过引用分配 new 的返回值是 已弃用 /home/saltushr/public_html/_classes/hr/dimedia/framework/Controller.class.php 第 26 行

代码是:

<?php
/**
 * This clas instantiates the required {@link Model} and {@link View} objects.
 *
 * @author Berislav Lopac berislav.lopac@dimedia.hr 
 * @version 1.0
 */
class Controller {
    var $model;   
    var $view;

    /**
     * The constructor.
     * Instantiates the {@link Model} and {@link View} objects.
     *
     * @param $_task    A String setting the base name of the instantiated objects.
     * @param $_db      ADOConnection object containing the connection to the database.
     * @param $_input   An Array with the input values required by the Model (usually the $_REQUEST array).
     */
    function &Controller($_name, &$_db, $_input) {

        $model = $_name . "Model";
        $view  = $_name . "View";
        $this->model =& new $model($_db, $_input);
        $this->view  =& new $view($this->model);
    }

    /**
     * Returns the associated {@link View} object.
     * @return View
     */
    function &getView() {
        return $this->view;
    }
}
?>

如您所见,第 25 行和第 26 行是空的:

21 变量 $model;
22
23 变量$视图;
24
25
26

27 /**

谁能帮我理解这是怎么回事?我在理解 PHP 方面还很陌生,可以使用一些帮助。 谢谢。

【问题讨论】:

  • 哦,我在格式化代码后弄乱了行号引用...但是这么多空行太可怕了。
  • 您应该在使用参考之前阅读手册:php.net/references
  • 我知道,问题是这段代码不是我的。我只是想删除已弃用的警告。我在 3 页上成功了,但这一页让我很烦。 @Felix Kling,ty 用于删除空行,我会更早地这样做。只有一个问题,当我对我得到的代码进行任何更改时:解析错误:语法错误,第 1 行 /home/saltushr/public_html/_classes/hr/dimedia/framework/Controller.class.php 中的意外 T_CLASS .. . 即使我只是复制上面的代码而没有额外的空行..
  • 好的,我解决了.. 删除了所有评论和空行,然后将 class Controller{ 放在

标签: php deprecated


【解决方案1】:

改变

$this->model =& new $model($_db, $_input);

$this->view  =& new $view($this->model);

$this->model =new $model($_db, $_input);

$this->view  =new $view($this->model);

【讨论】:

  • 我知道这一点,但是当我更改那行代码时,我收到有关同一页面的新错误:解析错误:语法错误,/home/saltushr/public_html/_classes/ 中的意外 T_CLASS第 1 行的 hr/dimedia/framework/Controller.class.php 我已经读到它与缺少“}”或其他一些错误有关,但我找不到任何东西..
【解决方案2】:

您正在分配一个新实例作为参考,根据设计,这不是一个好的解决方案。相反,只要对象请求特定资源(例如您的getViewmethod),就将对象作为引用传递。

一些对象必须保存初始实例。

【讨论】:

    猜你喜欢
    • 2012-12-21
    • 2010-11-08
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多