重写PHP函数-优化未定义变量通知的基础知识



需要建议,正如在第101行获得"未定义变量:tpl in/home/mytoys11/public_html/contensions/com_forms/controller.php"一样

    function toys(){
    // Create the view
    global $Itemid;
    $model = & $this->getModel('pages');
    $view = & $this->getView('pages', 'html');
    $view->setLayout('toys');
    // Push the model into the view (as default)
    $view->setModel($model, true);
    // Display the view
    $view->toys($tpl);
}

通过在最后一行中从视图中删除未定义的变量$tpl,可以像这样解决

    function toys(){
    // Create the view
    global $Itemid;
    $model = & $this->getModel('pages');
    $view = & $this->getView('pages', 'html');
    $view->setLayout('toys');
    // Push the model into the view (as default)
    $view->setModel($model, true);
    // Display the view
    $view->toys();
}

删除$tpl后,页面加载良好。我认为tpl是空字符串,但这是正确的方式还是函数优化不力,有什么建议吗。感谢

编辑谢谢,根据建议,这里的代码已被修改

    public function toys(){
    $model = $this->getModel('pages');
    $view = $this->getView('pages', 'html');
    $view->setLayout('toys');
    $view->setModel($model, true);
    $view->toys();
}

然而,它不适用于使用函数名作为:-

     displaytoys()

如果您不想处理视图的特定(子)模板,那么省略$tpl参数是可以且安全的。

不过,该代码还有其他几个问题。

  1. 未声明可见性。对于控制器中的一个操作,它应该是public
  2. 方法名称是动词,而不是名词
  3. 切勿使用global。甚至不使用CCD_ 4
  4. 不要评论显而易见的事实
  5. PHP4已不存在,因此默认情况下通过引用分配对象

所以你的代码应该是这样的:

public function displayToys()
{
    $model = $this->getModel('pages');
    $view  = $this->getView('pages', 'html');
    $view->setLayout('toys');
    $view->setModel($model, true);
    $view->displayToys();
}

为了使对displayToys的重命名生效,您还必须更改代码中的其他位置。无论您在哪里引用任务toys,都必须将其更改为displayToys。视图类中相应的方法也必须重命名。由于这只是一个样式问题,所以在第一步中可以不使用名称,而使用toys。你不会因此而出现功能问题。

最新更新