Share » Forums » Developer » Problems with foreach() and Return by...

Problems with foreach() and Return by Reference

Problems with foreach() and Return by Reference

Thursday 09 November 2006 2:14:27 am - 4 replies

Author Message

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/

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu