2015-11-04 21:34 阅读 44


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'));

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.


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongpangzan6425 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;
    点赞 评论 复制链接分享