doucai4274 2015-11-04 21:34
浏览 47
已采纳

PHP与&和Joomla严重错误

I'm helping a client troubleshoot some issues that have, so far as I can tell, arisen after their update to PHP 5.5 (Joomla is currently 2.5.20). The errors are getting thrown from some lines that are attributed to the Joomla CMS (though I don't think that's the reason). Either way, here are few code snippets and their respective errors that I need help with:

$app =& JFactory::getApplication();

PHP Strict Standards: Only variables should be assigned by reference...


public function getModel($name = 'Submission', $prefix = 'AwardAdminModel') 
        {
            $model = parent::getModel($name, $prefix, array('ignore_request' => true));
            return $model;
        }

getModel() should be compatible with JController::getModel($name = '', $prefix = '', $config = Array)


function display($cachable = false) 
    {
        JRequest::setVar('view', JRequest::getCmd('view', 'Submissions'));
        parent::display($cachable);
    }

display() should be compatible with JController::display($cachable = false, $urlparams = false)


Now I've never used ampersand with my PHP before but courtesy of the PHP manual I have some inclination as to what it does but I'm not sure why it's throwing a fit - I'm sure I could just remove it and be safe.

The getModel method is labeled as a proxy in the signature and sure enough, it calls it proper with 3 arguments within itself so I'm not sure why that's a problem.

Any helpful tips or solutions would be much appreciated.

Thanks.

  • 写回答

1条回答 默认 最新

  • dongpangzan6425 2015-11-04 22:43
    关注

    As a general rule, I would always advise against changing vendor code. It can create complications in future with vendor updates. Consider whether a lower version of PHP matching the vendor expectations may be suitable or investigate forking the vendors source repository so that future vendor updates can be merged it.

    To answer your question: The first error:

    PHP Strict Standards: Only variables should be assigned by reference...

    In PHP & is often used to assign aliases on items. This enables an item that may change to be known by more than one name and can sometimes be useful. It also used to be used in PHP to make it more efficient when using different names for the same item because PHP used to make copies every time something was assigned a new name.

    The efficiency of PHP in this latter scenario has been significantly improved since v5 and PHP are strongly encouraging the use of & for aliasing only variables and only when it makes sense logically for the purpose of the code The return value of a function cannot be aliased because logically it was inside the local scope of a function which no longer exists.

    So yes it is very likely safe to replace =& with = on function calls.

    Second error:

    getModel() should be compatible with JController::getModel($name = '', $prefix = '', $config = Array)

    This occurs because the function definitions do not match the parent class. There is a parent class which defines getModel with 3 arguments. This has been extended by the code you posted which defines getModel with just two arguments.

    The solution is to update the function definition to match the parent. This will make the error go away but would also possibly mis-lead future developers who may expect those extra arguments to have an effect or use the function in a way it wasn't intended; At least try to mitigate this by using ominous argument names:

    public function getModel($name = 'Submission', $prefix = 'AwardAdminModel', $unused = array()) 
    {
        $model = parent::getModel($name, $prefix, array('ignore_request' => true));
        return $model;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效