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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)
  • ¥100 某宝多次访问被拒绝,求解
  • ¥15 (标签-STM32|关键词-智能小车)