Forums / Developer / Problems with foreach() and Return by Reference

Problems with foreach() and Return by Reference

Author Message

Eirik Alfstad Johansen

Thursday 09 November 2006 2:14:27 am

Hi,

I'm experiencing the same problem as described in Dericks article "Reference Issues" (http://ez.no/community/articles/reference_issues), under the subheading "Problems with foreach() and Return by Reference."

I'm using v. 3.8.4, and my code looks something like this:

include_once( "lib/ezxml/classes/ezdomdocument.php" );

// create dom document
$doc = new eZDOMDocument( 'Root' );

// root
$root =& $doc->createElementNode( "Root" );
$doc->setRoot( $root );

// for each order
$i++;
foreach($items as $item)
{
	// order
	$xmlItem = $doc->createElementNode( 'Order' );
	
	// date and time
	$dateTime 		=& $doc->createElementNode( 'DateTime' );
	$dateTime->appendChild($doc->createTextNode( $i ));
	$xmlOrder->appendChild($dateTime);

	// append order to root
	$root->appendChild($xmlItem);
	
	$i++;
}

// return xml
return $doc->toString();

It this a bug in my code, or in eZ Publish, and what can I do to fix it?

Thanks in advance !

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Claudia Kosny

Thursday 09 November 2006 3:13:34 am

Hi Eirik

If you check the sample code in lib/ezxml/classes/ezdomdocument.php you can see that they don't use references here:

  $guppy = $doc->createElementNode( "Guppy" );
  $guppy->appendChild( $doc->createTextNode( "Guppy is a small livebreeder." ) );

whereas you use one:

$dateTime =& $doc->createElementNode( 'DateTime' );
$dateTime->appendChild($doc->createTextNode( $i ));

As far as I understand the way references work in PHP this should solve the problem.

Claudia

Eirik Alfstad Johansen

Thursday 09 November 2006 5:08:27 am

Hi Claudia,

Actually, that doesn't work (if you're talking about just removing the ampersand after the first equal sign). If you read the section of the article I'm referring to carefully, you'll see why.

However, thanks for helping. :)

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Claudia Kosny

Thursday 09 November 2006 6:32:46 am

Hi Eirik

That's the problem I usually have with references - you have to be really careful with what you are doing and I am usually more of the 'just try it' type.

In the current version of the code that Derick mentions (ezmatrixdefinition::xmlString() ) they simply unset the variable so they can reuse it reuse in the loop:

        foreach ( $this->ColumnNames as $columnName )
        {
            $columnNameNode = $doc->createElementNode( 'column-name' );
            $columnNameNode->appendAttribute( $doc->createAttributeNode( 'id', $columnName['identifier'] ) );
            $columnNameNode->appendAttribute( $doc->createAttributeNode( 'idx', $columnName['index'] ) );
            $columnNameNode->appendChild( $doc->createTextNode( $columnName['name'] ) );
            $root->appendChild( $columnNameNode );
            unset( $columnNameNode );
        }

This should probably work fine in your code as well as it breaks the connection between the variable and the content so the content is still there even if you reuse the variable.

Good luck

Claudia

Eirik Alfstad Johansen

Wednesday 06 December 2006 6:52:08 am

Hi Claudia,

Unsetting the variable worked like a charm. Thanks for the tip!

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

eZ debug

Timing: Jan 18 2025 21:19:04
Script start
Timing: Jan 18 2025 21:19:04
Module start 'content'
Timing: Jan 18 2025 21:19:04
Module end 'content'
Timing: Jan 18 2025 21:19:04
Script end

Main resources:

Total runtime0.1694 sec
Peak memory usage2,048.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0058 587.8438180.8125
Module start 'content' 0.00580.0057 768.656397.9609
Module end 'content' 0.01150.1578 866.6172526.8750
Script end 0.1693  1,393.4922 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00352.0909200.0002
Check MTime0.00140.8226200.0001
Mysql Total
Database connection0.00060.354410.0006
Mysqli_queries0.126674.76241410.0009
Looping result0.00130.74071390.0000
Template Total0.157593.010.1575
Template load0.00090.516710.0009
Template processing0.156692.468410.1566
Override
Cache load0.00060.369010.0006
Sytem overhead
Fetch class attribute can translate value0.00130.768310.0013
XML
Image XML parsing0.00030.152910.0003
General
dbfile0.00150.8600200.0001
String conversion0.00000.004130.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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs