On Tue, 2004-06-01 at 16:50, Florian Schaper wrote:
> Derick Rethans wrote:
> > On Tue, 1 Jun 2004, Florian Schaper wrote:
> >
> >> An Zend API 2.0 paper I read a while ago said something about
> >> "delete" being implemented in PHP5.
> >> However, no delete.
> >>
> >> Was this feature dropped? I have implemented delete for myself now
> >> and was wondering if it was worth committing.
> >
> > unset($obj) works just fine too.
> >
> 
> My version of delete works like the delete we know from C++. The object's
> destructor and the object is then released immediately. All references of
> this object are set to IS_NULL.

In comparison to the proposed "delete", unset() only decreases the
refcount.

Have a look at the following examples:

$ php-dev -r 'class Object { function __destruct() { echo "Destroyed\n";
}} $o= new Object(); unset($o); echo "Shutting down\n";'
Destroyed
Shutting down

$ php-dev -r 'class Object { function __destruct() { echo "Destroyed\n";
}} $o= new Object(); $o2= $o; unset($o); echo "Shutting down\n";'
Shutting down
Destroyed

In the second example, the destructor is not called until *after*
shutdown. This is the small but noticeable difference between what
Florian wants and what we currently provide.

While I agree this could be interesting, I'm opposed to a keyword named
"delete". Keywords may not be used as regular method names and a new
keyword "delete" would add more BC breaks for people using methods
called delete(). This:

  class DBConnection {
    function insert() { }
    function update() { }
    function select() { }
    function delete() { }
  }

is an example which can be seen in numerous applications. 

Then again, why doesn't unset() do this? That seems kind of inconsistent
with the object-handle-pass-by-value semantics we have in PHP5.

For the time being, using reference operators and setting the instance
to NULL gives you a workaround:

$ php-dev -r 'class Object { function __destruct() { echo "Destroyed\n";
}} $o= new Object(); $o2= &$o; $o= NULL; echo "Shutting down\n";'
Destroyed
Shutting down

Interesting enough (from a user's pov, not the Engine's), unset() will
not do the job:

$ php-dev -r 'class Object { function __destruct() { echo "Destroyed\n";
}} $o= new Object(); $o2= &$o; unset($o); echo "Shutting down\n";'
Shutting down
Destroyed

(neither will omitting the & in the first solution).

- Ti "back to references after all?" mm

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to