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 ) ); 
} 
?>
Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 03:11:55
Script start
Timing: Jan 18 2025 03:11:55
Module start 'layout'
Timing: Jan 18 2025 03:11:55
Module start 'content'
Timing: Jan 18 2025 03:11:55
Module end 'content'
Timing: Jan 18 2025 03:11:55
Script end

Main resources:

Total runtime0.0683 sec
Peak memory usage4,096.0000 KB
Database Queries23

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0057 587.7969152.6094
Module start 'layout' 0.00570.0031 740.406339.4141
Module start 'content' 0.00890.0580 779.8203451.6250
Module end 'content' 0.06690.0014 1,231.44538.3438
Script end 0.0682  1,239.7891 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00334.8090150.0002
Check MTime0.00121.7982150.0001
Mysql Total
Database connection0.00111.583410.0011
Mysqli_queries0.023334.0836230.0010
Looping result0.00020.3150210.0000
Template Total0.037554.920.0187
Template load0.00213.049620.0010
Template processing0.035451.807520.0177
Template load and register function0.00010.146710.0001
states
state_id_array0.00081.225810.0008
state_identifier_array0.00131.967320.0007
Override
Cache load0.00182.6004470.0000
Sytem overhead
Fetch class attribute name0.00152.261710.0015
class_abstraction
Instantiating content class attribute0.00000.017510.0000
General
dbfile0.00060.8190100.0001
String conversion0.00000.010540.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/article.tplextension/sevenx/design/simple/override/templates/full/article.tplEdit templateOverride template
1content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
5content/datatype/view/ezxmltags/header.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/header.tplEdit templateOverride template
9content/datatype/view/ezxmltags/emphasize.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/emphasize.tplEdit templateOverride template
11content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
8content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 37
 Number of unique templates used: 8

Time used to render debug report: 0.0001 secs