简体   繁体   English

在Joomla中处理E_STRICT

[英]Dealing with E_STRICT in Joomla

The line 线

$db =& JFactory::getDBO();

gives me this with E_STRICT E_STRICT给我这个

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

What exactly does this mean? 这到底是什么意思? I'm using JFactory just like it says in the docs. 我正在使用JFactory ,就像在文档中说的那样。 Should I be worried? 我应该担心吗?

Googling the error only gives me a bunch of Joomla people saying I should just disable E_STRICT . 谷歌搜索错误只会给我一大堆Joomla人说我应该禁用E_STRICT I'd rather not take the easy way out, as I'm trying to improve my skill. 我不想走简单的路,因为我正在努力提高自己的技能。

This is really a question for SO as this is not a review. 对于SO来说,这确实是一个问题,因为这不是审查。 However, if you were to have asked this there they would have downvoted this so quickly your head would have spun. 但是,如果您在这里问这个问题,他们会以最快的速度拒绝您的意见。 You should really google these kinds of things before asking for help, as this is well documented. 您确实应该在寻求帮助之前先用Google搜索这类东西,因为这已经有据可查。 That warning alone would have been enough to answer your question. 仅此警告就足以回答您的问题。

However, the reason for this error is because that is a class method, not a variable. 但是,此错误的原因是因为这是一个类方法,而不是变量。 And a static one to boot. 和静态启动。 That strict warning, which any warning or error should always be listened to, is telling you that there is nothing to reference. 严格的警告(任何警告或错误都应始终聆听)告诉您没有什么可参考的。 Referencing automagically duplicates any changes you make to $db and applies them to whatever variable it is referring to, effectively cloning it. 引用会自动复制您对$db所做的任何更改,并将它们应用于所引用的任何变量,从而有效地克隆它。 So now you begin to see a problem. 所以现在您开始看到一个问题。 You are not referencing a variable, as I said, you are referencing the return value of a method which is merely a section of the memory and cannot be referenced. 正如我所说,您不是在引用变量,而是在引用方法的返回值,该方法只是内存的一部分,无法引用。

So, you could do this: 因此,您可以这样做:

$temp = JFactory::getDBO();
$db =& $temp;

And it would work just fine. 它会很好地工作。 However, this is completely unnecessary. 但是,这完全没有必要。 Referencing is completely unnecessary here. 这里完全不需要引用。 What you really want to do is just set that method's return value to a variable and use that in the rest of your code. 您真正想要做的只是将方法的返回值设置为变量,并在其余代码中使用它。 Usually, especially for people just starting programming, referencing is not necessary. 通常,特别是对于刚开始编程的人来说,不需要引用。 You can accomplish the same thing by assigning the previous variable to a new variable, making the changes, then reassigning the new variable back to the old variable. 通过将先前的变量分配给新变量,进行更改,然后将新变量重新分配回旧变量,可以完成相同的操作。 Leave referencing alone until you learn a little more and can better understand it. 留下参考,直到您了解更多并能更好地理解它为止。 Even after years of programming I rarely use it. 即使经过多年编程,我也很少使用它。

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

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