Docs use assign by reference (=&) but is this necessary with PHP 5?

11

I've noticed in the Joomla documentation, such as for JFactory/getSession, that the examples use assign-by-reference (=&), rather than a simple assignment (=).

$session =& JFactory::getSession();

Is this just out-of-date, or purposefully covering users of PHP 4?

Is this necessary under PHP 5? Or is there something Joomla related that I've overlooked?

MrWhite

Posted 2014-04-28T10:24:14.943

Reputation: 259

Great question! I've often wondered if I should be using the equals symbol and ampersand or just the equals symbol. – TryHarder – 2014-04-28T11:15:38.063

Some docs are not updated. When you spot something, just hit the edit button, it's a wiki. – Valentin Despa – 2014-04-28T13:23:57.603

Answers

8

This was used for PHP4 times. From PHP5 onwards objects are assigned by reference, so there is no need to do that explicitly.

Dmitry Rekun

Posted 2014-04-28T10:24:14.943

Reputation: 3 692

1in php 5 it causes many strict standards warnings to have it as well, and there is a lot of it in the Joomla core, sometimes causing fun times when you swap to development error reporting. Another one of those changes in PHP that makes backwards compatibility hard. – Jordan Ramstad – 2014-04-28T12:52:18.087

It's bad in Joomla 1.5 since it was written to support PHP 4.3 up. With 2.5 and 3.x, the amount of E_STRICT errors in core is minimal if any. – Michael – 2014-04-29T00:25:20.820

1

Thanks, I had a assumed it must have been a PHP4 hangover, but with Joomla 1.6+ requiring PHP5+ I was beginning to wonder. Minor point, but objects are not actually "assigned by reference" in PHP5. The assign-by-reference operator is not reqd (in most cases) because class instances are stored differently in PHP5. (Class instances hold an "object identifier" rather than the value itself, as in PHP4.) An "object identifier" is assigned in PHP5, rather than the object value. The net result is similar, but there are differences.

– MrWhite – 2014-04-29T14:55:31.790