You are here:Home»KB»Web Design»CMS»Joomla»Extension Development»Strict Standards: Only variables should be assigned by reference
Monday, 28 December 2015 12:32

Strict Standards: Only variables should be assigned by reference

Written by

When developing a Joomla extension, and most likely a standalone extension you receive the following error or similiar:

Strict Standards: Only variables should be assigned by reference in /plugins/editors-xtd/executecode/script.php on line 22

Solution

The error is most likely down to a line similiar to this

$app =& JFactory::getApplication('administrator');

Notice the =&, in PHP5+ you should not longer use this. The line should look like this without the &

$app = JFactory::getApplication('administrator');

Explanation

Remove the & because as off PHP5 it is no longer required and is still only present in PHP5 for legacy code and will be removed in further iterations of PHP.

As of PHP 5, the new operator returns a reference automatically, so assigning the result of new by reference results in an E_DEPRECATED message in PHP 5.3 and later, and an E_STRICT message in earlier versions.

My understanding of this is that PHP5 automatically creates a reference to the objects value and does not copy the value, this makes =& redundant because you no longer need to manually specify this feature. With a reference if you alter the value in either the variable or the object value then the other is changed aswell, hence the term reference.

If you look in the Joomla index.php files you will see there is no =&

// Instantiate the application.
$app = JFactory::getApplication('site');

or in the administrator

// Instantiate the application.
$app = JFactory::getApplication('administrator');

Other Solutions

These are some other solutions I have come across that people use

  1. Append @ to the beginning of the statement, this causes PHP to supress errors for this statement
    @$app =& JFactory::getApplication('administrator');
  2. Supress errors via the php.ini file

These should be avoided as hiding errors does not mean they have been fixed.

Links

Read 938 times Last modified on Monday, 28 December 2015 20:31