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/

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 19 2025 00:30:49
Script start
Timing: Jan 19 2025 00:30:49
Module start 'layout'
Timing: Jan 19 2025 00:30:49
Module start 'content'
Timing: Jan 19 2025 00:30:49
Module end 'content'
Timing: Jan 19 2025 00:30:49
Script end

Main resources:

Total runtime0.8320 sec
Peak memory usage4,096.0000 KB
Database Queries63

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0068 588.0469152.6406
Module start 'layout' 0.00680.0043 740.687539.4766
Module start 'content' 0.01110.8193 780.1641560.7344
Module end 'content' 0.83040.0016 1,340.898416.1250
Script end 0.8319  1,357.0234 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00340.4035160.0002
Check MTime0.00140.1704160.0001
Mysql Total
Database connection0.00150.180210.0015
Mysqli_queries0.764991.9360630.0121
Looping result0.00070.0859610.0000
Template Total0.789894.920.3949
Template load0.00190.230320.0010
Template processing0.787994.700620.3939
Template load and register function0.00010.012510.0001
states
state_id_array0.00140.173210.0014
state_identifier_array0.00120.140520.0006
Override
Cache load0.00160.1970350.0000
Sytem overhead
Fetch class attribute can translate value0.00060.070420.0003
Fetch class attribute name0.00220.264760.0004
XML
Image XML parsing0.00070.078620.0003
class_abstraction
Instantiating content class attribute0.00000.002280.0000
General
dbfile0.00100.1153160.0001
String conversion0.00000.001240.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/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
3content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
5content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
9content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
4content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 23
 Number of unique templates used: 6

Time used to render debug report: 0.0001 secs