Learn / eZ Publish / Reference Issues

Reference Issues

Returning from a Function

The following code will now throw a PHP notice on the return statement.

<?php 
function &byReference($value, &$a, &$b) { 
     if ($value$a){   
          return($a); 
} 
?>

This is because using () around a variable creates a statement out of it. It is only possible to return variables by reference, and not the temporary evaluated value of a statement.

Ternary Operator

Similarily, the following code will also throw a notice on the return statement:

<?php 
function  &byReference($value, $a, $b) { 
     return$value ? $a : $b; 
} 
?>

Here it seems that you're returning either the variable $a or $b by reference, but that is not the case as you're returning the evaluated value of $value ? $a : $b, which is not a variable but the result of the evaluated statement. Similarily to the first example this will not work

Returning a Returned Reference

<?php 
function &byReferenceA(){ 
     return $var } 
function &byReferenceB(){ 
     return byReferenceA(); 
} 
?>

This is not allowed, as in return byReferenceA();byReferenceA() is a statement (returning a temporary value), not a variable.

Problems with foreach() and Return by Reference.

This is an exaple that I took from a recent problem that we found in ezmatrixdefinition::xmlString(). See the following excerpt from our code:

<?php 
foreach ( $this->ColumnNames as $columnName ) 
     {  $columnNameNode =& $doc->createElementNode( 'column-name' );/* [1] */  
        $root->appendChild( $columnNameNode );                      /* [2] */ }   
/* 'createElementNode' has prototype:   
* function createElementNode( ... ). */   
     function &appendChild( &$node ) {  
          if ( <a href="http://www.php.net/get_class">get_class</a>( $node ) == "ezdomnode" )  
                {   $this->Children[] =& $node;                            
                     return $node;  
}  
      return false; 
} 
?>

As the result xml document contains nodes with the value of last appened child:

<ezmatrix> 
<column-name id="col_1" idx="1">Col_1
</column-name> <column-name id="col_1" idx="1">Col_1
</column-name> </ezmatrix>

What happens here is:
In [1] $columnNameNode is assigned a new value (and not by reference because createElementNode() returns by value. In two the $columnNameNode is passed as reference to appendChild() where it is assigned by reference to Children[]. In the next iteration this referenced variable is assigned a new value in [1]. Because the Children[] array's first element is still a reference to $columnNameNode, this element is updated too. Then in [2]/[3] it is assigned by reference to Children[] again. Then this cycle starts over again.

Assigning to $this

Although I'm not exactly sure why this did work with PHP 4.3, the code was previously wrong too. Here is an example of the old code:

<?php function removeForContentObjectAttribute( $contentObjectAttributeID ) {  
if ( !<a href="http://www.php.net/isset">isset</a>( $this ) or <a href="http://www.php.net/get_class">get_class</a> ( $this ) != 'ezimagefile' )   
     $this =& eZImageFile::instance();  
$this->remove( <a href="http://www.php.net/array"> array</a>
(    'contentobject_attribute_id' => $contentObjectAttributeID ) ); 
} 
?>

In the code above you see that we're reassigning $this to something else, if it did not point to an object of the class ezimagefile. Well, this does not work correctly if this function was called from another object's method as:

<?php 
eZImageFile::removeForContentObjectAttribute( 42 ); 
?>

Because in PHP 4 $this will then point to the previous scope's object, and thus replacing that object instead. For some reasons because of the memory corruptions going on in PHP 4.3 this did not cause any problems. The new correct code looks like:

<?php function 
removeForContentObjectAttribute( $contentObjectAttributeID ) 
{  
     if ( <a href="http://www.php.net/isset">isset</a>( $this ) and <a href="http://www.php.net/get_class">get_class</a>( $this ) == 'ezimagefile' )   
          $instance =& $this;  
     else   $instance =& eZImageFile::instance();  
            $instance->remove( <a href="http://www.php.net/array">array</a>(    
           'contentobject_attribute_id' => $contentObjectAttributeID ) ); 
} 
?>

Article Discussion

Reference Issues