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.