|
Posted by Blueparty on July 10, 2008, 2:04 pm
Please log in for more thread options Jerry Stuckle wrote:
> Juergen-Bernhard Adler wrote:
>> Hello,
>>
>> pretend some noob has (in a fake-static class) provided the following
>> method
>>
>> public static kill_object($obj)
>> {
>>
>> if (!is_object($obj))
>> return false;
>>
>> $obj = null;
>>
>> return true;
>>
>> }
>>
>> Obviously noob is heading at destroying an object!
>>
>> Does he succeed?
>>
>> Let's see:
>>
>> <code>
>>
>> $blubber = new crazy_object...;
>>
>> ... some schnickschnack...
>>
>> if (!static_class::kill_object($blubber))
>> exit(0);
>> print_r($blubber);
>>
>> if (isset($blubber))
>> print "I'm Blubber\n";
>> else
>> print "I'm Blubber no more\n";
>>
>>
>> </code>
>>
>> noob would expect to see no print_r-output and the message should be
>>
>> "I'm Blubber no more"
>>
>> Actually, things are quite different:
>>
>> 1st: Object is printed per print_r
>>
>> 2nd: Message is "I'm Blubber"
>>
>> Haven't I just killed the object...? Objects, when supplied as parameter,
>> are passed by reference in php5, aren't they?
>>
>> OK, that obviously did not work (somehow... - remember, I'm a noob).
>>
>> I reformulate the static kill_object-method:
>>
>> public static kill_object(&$obj)
>> {
>>
>> if (!is_object($obj))
>> return false;
>>
>> $obj = null;
>>
>> return true;
>>
>> }
>>
>> Note, that there comes the old (deprecated) reference-operator '&'
>> (that -
>> for objects - is no longer needed in php5, right?)
>>
>> This time, however, the result is:
>>
>> 1st: Object is no longer printed (per print_r)
>>
>> 2nd: Message is "I'm Blubber no more"
>>
>> Please assist!
>>
>> cheers
>>
>> juergen
>>
>>
>
> First of all, static methods don't work on objects - they work on the
> class.
>
> But PHP will destroy an object sometime after the last reference to the
> object is removed. If this is your ONLY reference to the object, at
> some time, when the PHP garbage collector runs, the object will be deleted.
>
> But this isn't necessarily the only reference to the object, and the
> garbage collector won't necessarily run immediately.
>
> It's not like C++ or SmallTalk, where the destructor can destroy an
> object immediately.
>
In fact, when the reference to an object are set to NULL, the
object should be inaccessible from that reference. From the
programmers point of view, it should be the same object destroyed.
System may have need to keep the actual data, but that is the benefit of
GC, programmer does not need to manage memory allocation.
B
|