faqts : Computers : Programming : Languages : PHP : Database Backed Sites : MySQL : Questions in search of an Answer

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

21 of 24 people (88%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Is there a destructor call for php classes? (Like the c++ ~class)

Sep 17th, 2004 06:19
Paul Wieland, Zlatin Zlatev, Jens Clasen, Marshall Mattingly, http://www-user.tu-chemnitz.de/~meal/pear/pd2pm/core.pear.pear.intro.html **** http://www.phpworld.com/manual/class.pear.html **** http://www.php.net/manual


Unfortunately the concept for objects in PHP is a bit lazy. There are no
destructors, no public or static methods values, what so ever.
Something like that is planned for future releases but that's far from
being implementet.
If You need to do some cleanup you'll have to do so on your own.
____
Update (2021-02-21) Zlatin Zlatev:
Also you may try to use for this purpose code like this:
class myclass {
    var $classname;
    function myclass() {
       $args = func_get_arg(0);
       if (isset($this->classname))
          $this->classname = get_parent_class($this->classname);
       else
          $this->classname = get_class($this);
       if (method_exists($this, "_".$this->classname)) {
          register_shutdown_function(array($this, "_".$this-
>classname));
       }
       if (method_exists($this, get_parent_class($this->classname))) {
          $classname = $this -> classname;
          $this = call_user_func(array($this, get_parent_class($this-
>classname)), $args);
          $this -> classname = $classname; //preserve classname
       } //Call parent class constructor if any.
       return $this;
    }
    function _myclass() {
    }
} // end of class myclass
However I am testing this now and I am not sure, whether this can be 
called a "destructor" in true OOP meaning.
_______
Oh... I made a search in Google and as I see simmilar approach is used 
by PEAR baseclass too. (I have updated the code so it uses pear 
convention for naming destructor).
In order to enable inheritance and passing of parameters between 
constructors parameters to constructor are passed in associative array. 
e.g.:
myfileobj = new myfile(array(filepath=>'/etc/tmp/', 
filename=>'test.txt'));
Also some issues with call_user_func were addressed in 
http://www.php.net/manual/en/function.call-user-func.php
For constructors and destructors see also:
http://www.php.net/manual/en/language.oop.constructor.php
=======================
PHP5 now has a built in destructor mechanism as well as public, private and  protected 
variables.
Read here: http://www.php.net/zend-engine-2.php