On Sat, 2004-08-21 at 00:10, Curt Zirzow wrote: > * Thus wrote Robert Cummings: > > Hi All, > > > > I think I'm looking for something that doesn't exist, but just in > > case thought I'd check the list. Does anyone know if a PHP function > > exists to get the number of references on a given variable's data? I was > > hoping to create a way for a factory to automatically recycle resources > > without the need for the developer to call some kind of free() method. > > If I could get the internal reference count then I'd be able to > > determine if it is free by virtue of only 1 reference (the factory). > > This is for PHP4 btw, the solution is trivial in PHP5 using destructors. > > unfortantly there isn't a method to determain this. > > Be careful with PHP5, i'm not sure if its applicable in your > situation, but there does seem to be rumor that php5 objects are > assigned by reference, which isn't true: > > $o1 = new object(); > $o2 = $o1; > unset($o2); > > the Object still exists, and the destructor isn't called. > > vs. > > $o3 = new object(); > $o4 =& $o3; > unset($o3); > > The object will no longer exist, destructor is called. > > > To elaborate a bit more on this... objects in php5 add a new > layer to variable existance. > > When an object is created it creates itself into its own memory > space and a new variable ($o1) is allocated to point to that > objects memory space. > > When the variable ($o1) is assigned to another variable ($o2), a > memory is allocated to point to the object as well. So now > two allocated variables are pointing to the same object. When one > of the variables is destroyed, since another variable still points > to the object, the object will continue to exist. Until all > variables pointing to that object no longer exits. > > In unix filesystem terms, a variable of an object is very much like > how a hard link is treated. The allocated filename (variable in > php's case), points to the allocated data (object). When no other > filename points to the same data, the data is released from usage. > > In the later example (using =&), the new variable created ($o4), > points to ($o3). So, as the manual explains, the new variable > ($o4) is a symoblic link to the original variable ($o3).
Hmmm, I think you got some wires crossed :) Your two examples are identical in functionality; except that your assertion that the object no longer exists in example 2 is incorrect. In PHP5 object assignment results in a reference assignment instead of a copy as in PHP4. Try running the following sample script: <? class Foo { var $foo = 0; function __destruct() { echo 'Destruction: '.$this->foo."\n"; } } $o1 = new Foo(); $o2 = $o1; $o1->foo = 1; echo "Checkpoint 1\n"; unset( $o1 ); echo "Checkpoint 2\n"; unset( $o2 ); echo "Checkpoint 3\n"; $o3 = new Foo(); $o4 =& $o3; $o3->foo = 2; echo "Checkpoint A\n"; unset( $o3 ); echo "Checkpoint B\n"; unset( $o4 ); echo "Checkpoint C\n"; So to take your example of symbolic links. When a new object is created the memory is allocated and a symbolic link is created to that memory. Then when you assign the object to another variable then another symbolic link is created. So in your examples the mere assignment of the newly created object: $o1 = new object(); creates a symbolic link. Thus when the __destruct() method is automatically called I can be certain that no "symbolic links" exist. So for my factory example the __destruct() method could inform the factory that the the resource consumed by the object being destroyed is now re-usable. In PHP5 to get a copy of, versus a reference to, an object the developer must call the __clone() method for the target object: $obj = new Foo(); $copyNotReference = $obj->__clone(); Cheers, Rob. -- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php