Re: "waldschrotts guide to nifty references" - manual pagedraft, version 0.9b

From: Date: Sun, 20 Aug 2000 02:52:16 +0000
Subject: Re: "waldschrotts guide to nifty references" - manual pagedraft, version 0.9b
References: 1  Groups: php.dev php.qa 
Request: Send a blank email to php-dev+get-29761@lists.php.net to get a copy of this message
André Langhorst wrote: > Please read carefully and correct grammatical and syntactical woes (typos too)((engl. is not my > native language)) Great work. Suggested edits: References in PHP - Creating aliases for variables ------------------------------------------------------------------------ You can create multiple aliases for the same variable in PHP, by using the "&"-sign to establish other references to it.. "&" tells PHP to create another reference to the already existing variable . It´s a second reference, because you´ve created the first reference by creating the variable. This variable exists apart from regular variable names, and persists as long as one variable name exists to access it. Creating further references is like having a chamber and creating more and more doors to enter it. If you´ve no door to enter, it´s useless and it does not exist anymore by definition. The name reference can be a bit misleading as you´re expecting that a reference itself is somehow different from a variable, but you cannot distinguish which one is a reference and which one is a variable. References are different from pointers in C, and Java references, but are very similar to references in C++. Example: $foo=5; // assign the value 5 to a variable accessible through $foo $boo=&$foo; // create reference $boo to the variable accessible through $foo from outside a function // now that variable is accessible through $foo AND $boo equally $boo=6; // by changing $boo, $foo is changed too, because they´re the same variable echo $foo; // both are referencing to the same variable with the value 6 References as function parameters -------------------------------------------------- Function parameters can be referenced for variables passed to the function call. Simply prepend the function parameter with a "&"-sign. $foo=5; // assign the value 5 to a variable accessible through $foo function foo(&$boo) { // create reference $boo to that variable accessible through $foo from outside the function // now that variable is accessible through $foo AND $boo equally $boo=6; } // by changing $boo, $foo is changed too, because they´re the same variable echo $foo; // both are referencing to the same value, 6. Returning references ------------------------------ You´re also able to return references, so variables can survive function calls without using global variables. Prepending the function name with the "&"-sign indicates that you´re going to return a reference. It´s not sufficient to assign that reference afterwards with the assignement operator, like you´d do it with the new() statement, you have to tell PHP explicitly that it should assign this reference again. function &foo(&$boo) { // tell function to return a refernce, create reference $boo // to that variable accessible through $foo from outside the function $boo=6; // by changing $boo, $foo is changed too, because they´re the same variable return $boo; } // return the variable to outside $foo=5; // assign the value 5 to a variable accessible through $foo $foobar=&foo($foo); // create reference $foobar to the reference returned by foo() echo $foobar; // the variable is now accessible through $foobar and $foo from the global scope // it was accessible through $boo from the function scope, but this reference does not exist // anymore, because the function has finished $foobar=7; // to prove that $foobar and $foo are the same, we´ll asign the value 7 to them echo $foo; // so far we´ve had access to the same variable the whole time, at the beginning, inside, // and now at the end of the function, the only difference is that it happened through // different references Common pitfalls ----------------------- Re-Referencing of multiple variable names, bound to a certain variable, to be referenced to another variable, does not work in PHP, if you´ve understood the concept of references in PHP you´ll understand why. Example: function foo(&$boo) { // create reference $boo to that variable accessible through $foo from outside the function $barfoo=3; // assign the value 3 to a variable accessible through $barfoo $boo=&$barfoo; // you might expect that $boo and $foo are referenced to $barfoo, but this is not the case // as the relation to between $boo and $foo is now broken. $boo is now referenced // to the variable accessible through $barfoo. return $boo; } // return the value of $boo to prove that it is 3 and not 5 $foo=5; // assign the value 5 to a variable accessible through $foo echo foo($foo); // echo the returned value from foo() Circular references --------------------------- Thanks to these indirectional pointers (references) it´s possible to create true recursive data structures, which can be very handy in object-orientated communication mechanisms. Simple example:: class foo { var $a=1; var $foobar; // this property will be a reference to another object later function reference(&$boo) { // make $boo a reference $this->foobar=&$boo; } } // make $this->foobar another reference to what $boo // references too (passed object) class foobar { var $a=2; var $foo; // this property will be a reference to another object later function reference(&$boobar) { // make $boobar a reference $this->foo=&$boobar; } } // make $this->foo another reference to what $boobar // references too (passed object) $object_foo=new foo(); // create an instance of class foo $object_foobar=new foobar(); // create an instance of class foobar $object_foo->reference($object_foobar); // invoke the referencing mechanism, step 1, $object_foo->foobar is referenced to $object_foobar $object_foobar->reference($object_foo); // invoke the referencing mechanism, step 2, $object_foobar->foo is referenced to $object_foo // circular reference established //Now that we´ve created a circular reference we´re able doing weird things like.. echo $object_foo->a; echo $object_foo->foobar->a; echo $object_foo->foobar->foo->a; echo $object_foo->foobar->foo->foobar->a; echo $object_foo->foobar->foo->foobar->foo->foobar->foo->foobar->foo->foobar->foo->a; // ... etc. :) Note: The use of circular references causes memory leaks which may be cleaned up after each request in same cases (the more complex, the greter likelyhood of a leak), This is intended and should not hurt unless you´re using extremely large objects, or having many concurrent scripts running. -- Brought to you from iBop the iMac, a MacOS, Win95, Win98, LinuxPPC machine, which is currently in MacOS land. Your bopping may vary.

« previous php.dev (#29761) next »