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

eZ debug

Timing: Jan 18 2025 00:01:59
Script start
Timing: Jan 18 2025 00:01:59
Module start 'content'
Timing: Jan 18 2025 00:02:00
Module end 'content'
Timing: Jan 18 2025 00:02:00
Script end

Main resources:

Total runtime0.2360 sec
Peak memory usage4,096.0000 KB
Database Queries165

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0092 588.7266180.8438
Module start 'content' 0.00920.0924 769.5703462.2578
Module end 'content' 0.10160.1344 1,231.8281399.5313
Script end 0.2360  1,631.3594 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00431.8109210.0002
Check MTime0.00150.6351210.0001
Mysql Total
Database connection0.00411.724710.0041
Mysqli_queries0.145361.55651650.0009
Looping result0.00150.61951630.0000
Template Total0.194482.420.0972
Template load0.00241.010420.0012
Template processing0.192081.342920.0960
Template load and register function0.00010.045410.0001
states
state_id_array0.00180.780410.0018
state_identifier_array0.00210.887920.0010
Override
Cache load0.00190.8152470.0000
Sytem overhead
Fetch class attribute name0.00080.321210.0008
Fetch class attribute can translate value0.00100.438010.0010
class_abstraction
Instantiating content class attribute0.00000.002110.0000
XML
Image XML parsing0.00030.113110.0003
General
dbfile0.00281.1693220.0001
String conversion0.00000.002830.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 37
 Number of unique templates used: 8

Time used to render debug report: 0.0002 secs