* 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).


I hope that wasn't too much info :)


Curt
-- 
First, let me assure you that this is not one of those shady pyramid schemes
you've been hearing about.  No, sir.  Our model is the trapezoid!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to