2

I'm using joomla and i have read through the API and I noticed the JFactory class having functions return a reference to the object but the examples I have gathered from the internet do not use the & when using the functions.

Say for example the Jfactory::getSession() which returns a reference to the global session. the documentation shows that its defined as function &getSession(params){code} - clearly defining a reference return. however, in this example, they call it as $session = JFactory::getSession(); shouldn't it be $session =& JFactory::getSession();?

It states here in the php documentation that there is an & in the function and an =& in the caller. I have also gone through C programming and if I miss things like this, there will be errors like "invalid pointer conversion" - which is not a good programming practice to tolerate.

What is the proper practice?

Additional info:

i use joomla 1.7 and i'm creating a component. i work on xampp with php 5.3.8

Joseph
  • 117,725
  • 30
  • 181
  • 234
  • 3
    Modern PHP versions always return objects as references, so the `=&` is not necessary anymore, and will actually throw out a warning message. Code which uses `=&` is either badly written, old/obsolete, or trying to working on as many PHP versions as possible, and is therefore by definition badly written anyways. – Marc B Dec 13 '11 at 14:29
  • This just reminds me how terrible the Joomla actually is. – tereško Dec 13 '11 at 14:34
  • possible duplicate of [PHP Framework support for legacy PHP4?](http://stackoverflow.com/questions/1714023/php-framework-support-for-legacy-php4) – mario Dec 13 '11 at 14:42
  • possible duplicate of [What does =& mean in PHP?](http://stackoverflow.com/questions/2081806/what-does-mean-in-php) – mario Dec 13 '11 at 14:44
  • possible duplicate of [php.net: Objects and references](http://php.net/manual/en/language.oop5.references.php) – mario Dec 13 '11 at 14:47
  • 2
    Careful, there, returning by reference is not the same as returning an object cos the latter allows only changing the object but returning by reference allows changing the variable completely to another object, a scalar or whatnot. – chx Dec 13 '11 at 14:52
  • I completely understand the idea of references (pointers in C, if i may) but my question was if the function was defined with the `&`, **is it necessary** to call it like `$variable =& functionName()` – Joseph Dec 13 '11 at 14:57
  • @chx what `Jfactory::getSession()` does is returns a reference to the object. by what i understood in the documentation, the object is existing and that function returns a reference to existing object. – Joseph Dec 13 '11 at 15:02

1 Answers1

1

It depends on:
- the version of Joomla and the version of PHP you use
- what returns the JFactory::getSession() method

Joomla version 1.5 is compatible with PHP 4 and PHP 5, versions 1.6 and 1.7 are only compatible with PHP 5.

If the method returns an object, the & is mandatory in PHP 4: by default, objects are passed/returned by value (a copy of the object occurs). The & avoids the copy.
In PHP 5, the & is useless : objects are always passed/returned by reference (no copy occurs).

If the method returns anything else, the & shouldn't be used but can be useful in some very rare cases (in order to save memory if you have a huge array or string, for example, and you don't want of copy of them in the assignment).

The good practice is to always put a & in assignment when the method signature includes a & too.

In your case, I think that you're using PHP 5 and a recent version of Joomla, so don't use & : this is probably an obsolete code in Joomla sources.

CedX
  • 3,854
  • 2
  • 36
  • 45
  • i suppose the online documentation is obsolete. i don't see the `&` in the actual source. found the documentation here: http://api.joomla.org/Joomla-Framework/JFactory.html – Joseph Dec 13 '11 at 15:12