4

The line

$db =& JFactory::getDBO();

gives me this with 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. Should I be worried?

Googling the error only gives me a bunch of Joomla people saying I should just disable E_STRICT. I'd rather not take the easy way out, as I'm trying to improve my skill.

SomeKittens
  • 38,868
  • 19
  • 114
  • 143

1 Answers1

5

This is really a question for SO as this is not a review. 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. 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. 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.

mseancole
  • 1,662
  • 4
  • 16
  • 26
  • ...You do realize that I googled it, like I said in the question? Also, the *docs* say to use references, which can be quite handy. – SomeKittens Aug 16 '12 at 20:43
  • I meant if you just googled that error message, not that line of code. However, I did miss that you said you googled the code, my apologies. I'm not saying "don't use references" I'm saying don't START with references. Yes, they are a powerful tool, yes they are quite nifty. But they are more advanced and can lead to problems with code legibility. After all, who's looking for a tiny "&" in a huge pile of code? I tend to avoid them, the only "real" reason to use them is when your code demands top performance because it is doing very heavy processing elsewhere. Otherwise there are other ways. – mseancole Aug 16 '12 at 23:51
  • @SomeKittens: I found this [same question](http://stackoverflow.com/questions/11777908/strict-standards-only-variables-should-be-assigned-by-reference-php-5-4) on second page of results. But Google has some neat tools to help, for instance: `-intext:Joomla`. Returns results that don't contain Joomla. With this search it was the second link. Don't stop on the first page, Google is an excellent tool if used properly. Here's a link to some [advanced features](http://www.googleguide.com/advanced_operators.html). – mseancole Aug 17 '12 at 15:56