Re: [PHP-DEV] References - good or bad
Hi, I've read PHP 4: Reference Counting and Aliasing By Andi Gutmans on Zend.com site (http://www.zend.com/zend/art/ref-count.php). Also I read this mail http://marc.theaimsgroup.com/?l=php-devm=100955714924477 from him. I would like to know more about how reference is managed by PHP4. I'm missing something like that article (see the first URL) for reference. What happens inside PHP4 with this code ? ? $a = 5; $b = $a; $c = $b; $d = $c; ? How PHP4 know that $c and d are reference to $a and not counted like $b ? And so what makes references so bad ? ;-) Another (more important) example: ? function foo( $msg ) { $tmp = $msg; return $tmp; } ? What happens to $tmp ? If it will not refer to a new memory block why the memory used by the script is more than without '' ? (See the second URL). Thanks. Ed ___ Build high quality traffic with the Web's Premier traffic building system. 2 to 1 ratio! http://www.itrafficstar.com/?ref=6 -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] References - good or bad
At 08:20 AM 12/29/2001 +0800, Alan Knowles wrote: Andi Gutmans wrote: As I mentioned on the ZE2 mailing list there general rule of thumb is: a) Objects should be passed by reference. b) Everything else including arrays should be used by value whenever possible semantically. In the ZE2 objects will join b). Is there a proposed sytnax to stop copy by reference (Or did i miss it in the ZE2 docs) old stuff $object_copy = $object; //(copy) $object_copy = $object; //(copy reference) new stuff? $object_copy = copy_object($object); //(copy) $object_copy = $object; //(copy reference) To copy around a reference you will just use it as a regular value. Like in your second example, i.e.: $same_object = $object; Basically you're just copying the object handle and not the object itself. In order to create a real copy (with a new object handle) you'll do: $new_object = $object-__clone(); Andi -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] References - good or bad
As I mentioned on the ZE2 mailing list there general rule of thumb is: a) Objects should be passed by reference. b) Everything else including arrays should be used by value whenever possible semantically. In the ZE2 objects will join b). Basically what this means, as long as you're not changing the data passing it by value will take the full advantage of reference counting. Andi At 10:28 AM 12/28/2001 -0600, Brian Moon wrote: Ok, there has been some discussion on the ZE2 list about returning references from functions and it has gotten me looking at references in general. Phorum deals with some pretty large arrays and so far that has made us faster than other BB's. I want to keep it that way. First, the question: When is it a good idea to use references for performance reasons? Now, some things I tried (PHP 4.0.6): $arr=array(); $arr=array_pad($arr, 1, md5(microtime())); function test($arr){ $newvar=$arr; return $newvar; } $newvar=test($arr); This code takes 2.6MB of ram to run. Changing the function to any of these: function test($arr){ $newvar=$arr; return $newvar; } function test($arr){ $newvar=$arr; return $newvar; } function test($arr){ $newvar=$arr; return $newvar; } it now takes 4.4MB to run. That is not what I expected. Now, this function: function test($arr){ foreach($arr as $key = $var){ $newvar[$key]=$var; } return $newvar; } takes 4.4MB also. I would expect that. However, changing it to: function test($arr){ foreach($arr as $key = $var){ $newvar[$key]=$arr[$key]; } return $newvar; } It now takes 6.8MB of ram. Last, this code: $arr=array(); $arr=array_pad($arr, 1, md5(microtime())); $arr2=$arr; uses 2.6MB where: $arr=array(); $arr=array_pad($arr, 1, md5(microtime())); $arr2=$arr; uses 3.5MB. So, my conclusion is that references are bad in all cases on memory and should only be used when you have to know you are using the same exact data in two places, or a variable needs to be modified by a function. If this is the case, shouldn't this be documented? Am I missing something? Brian Moon -- dealnews.com, Inc. Makers of dealnews dealmac http://dealnews.com/ | http://dealmac.com/ -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] References - good or bad
Andi Gutmans wrote: As I mentioned on the ZE2 mailing list there general rule of thumb is: a) Objects should be passed by reference. b) Everything else including arrays should be used by value whenever possible semantically. In the ZE2 objects will join b). Is there a proposed sytnax to stop copy by reference (Or did i miss it in the ZE2 docs) old stuff $object_copy = $object; //(copy) $object_copy = $object; //(copy reference) new stuff? $object_copy = copy_object($object); //(copy) $object_copy = $object; //(copy reference) regards alan Basically what this means, as long as you're not changing the data passing it by value will take the full advantage of reference counting. Andi -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]