简体   繁体   English

&和Joomla导致PHP严格错误

[英]PHP Strict errors with & and 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). 据我所知,我正在帮助客户解决一些问题,这些问题是在更新到PHP 5.5(Joomla当前为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). 错误归因于Joomla CMS的某些行(尽管我不认为这是原因)。 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... PHP严格标准:仅变量应通过引用分配...


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) getModel()应该与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) display()应该与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. 现在,我以前从未在我的PHP上使用“&”符号,但是由于PHP手册的缘故,我对它的用途有所了解,但是我不确定为什么它会变得合适-我敢肯定我可以将其移除并且安全。

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. getModel方法在签名中被标记为代理,并且足够肯定,它在自身内部带有3个参数的情况下调用它是正确的,因此我不确定为什么会出现问题。

Any helpful tips or solutions would be much appreciated. 任何有用的提示或解决方案将不胜感激。

Thanks. 谢谢。

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. 考虑一个符合供应商期望的较低版本的PHP是否合适,或研究对供应商源存储库进行分叉,以便将来的供应商更新可以合并到该版本中。

To answer your question: The first error: 回答您的问题:第一个错误:

PHP Strict Standards: Only variables should be assigned by reference... PHP严格标准:仅变量应通过引用分配...

In PHP & is often used to assign aliases on items. 在PHP中, &通常用于为项目分配别名。 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. 在同一项目中使用不同名称时,它还曾经在PHP中使用,以使其效率更高,因为PHP每次给新名称分配名称时都会进行复制。

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. 在后一种情况下,PHP的效率得到了显着提高,因为v5和PHP强烈鼓励使用&来仅对变量进行别名,并且仅在出于代码目的在逻辑上有意义时才使用函数的返回值,因为从逻辑上讲,它位于不再存在的函数的本地范围内。

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) getModel()应该与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. 有一个父类,它定义带有3个参数的getModel。 This has been extended by the code you posted which defines getModel with just two arguments. 您发布的代码对它进行了扩展,该代码仅使用两个参数定义了getModel。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM