note 57194 added to language.references.pass

From: Date: Mon, 26 Sep 2005 22:24:51 +0000
Subject: note 57194 added to language.references.pass
Groups: php.notes 
Request: Send a blank email to php-notes+get-95825@lists.php.net to get a copy of this message
I was tempted to refactor bobbykjack's sample by 1st changing $null to null (which I think is better semantiquely than '' even if equivalent). However I noticed that this could produce the same error (well behavior that lead to think there is an error) that make me come here in the first place. Just follow the code and notice how I reuse the $null and then set the parent node to the grand_parent. Yes, I have not done it by reference intentionnally. However, the solution will either be to keep the if and never reference the $null else always set a variable by reference when it need to. But there is case where you want to affect a variable directly and do not forget that it reference the same memory than maybe a lot of variable. However, always be aware that if you pass by reference. If you modify the variable (and not affect a new reference to the variable name) that you are affecting all variable that reference the same data. <?php /** * To test the discussion at http://www.php.net/manual/en/language.references.pass.php */ class tree { var $parentNode; var $childNodes; function tree(&$parentNode) { //if (isset($parentNode)) //{ $this->parentNode =& $parentNode; //} } } $null = null; $parent = new tree($null); $child = new tree($parent); echo '<pre>'; print_r($child); echo '</pre>'; $grand_parent = new tree($null); $parent->parentNode = $grand_parent; // if I was doing it by reference here, all would work fine echo '<pre>'; print_r($null); echo '</pre>'; ?> ---- Manual Page -- http://www.php.net/manual/en/language.references.pass.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57194 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57194&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57194&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57194&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57194&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57194&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57194&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57194&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57194&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95825) next »