What determines when a class object is destroyed in PHP?
Let's say that we have class CFoo . In the following example when is CFoo::__destruct() called? function MyPHPFunc() { $foo = new CFoo(); . . . // When/where/how does $foo get destroyed/deleted? } In this example would the destructor be called when the script exits the scope of MyPHPFunc because $foo would no longer be accessible? In PHP all values are saved in so called zval s. Those zval s contain the actual data, type information and - this is important for your question - a reference count. Have a look at the following snippet: $a = new B; // $a points to zval(new B) with refcount=1 $b =