note 94894 added to language.oop5.decon

From: Date: Tue, 01 Dec 2009 14:13:39 +0000
Subject: note 94894 added to language.oop5.decon
Groups: php.notes 
Request: Send a blank email to php-notes+get-163449@lists.php.net to get a copy of this message
Destructors are called in a stupid first-in-last-out stack order and do not honor any referential dependencies. The following example works fine as long as you instantiate ClassB after ClassA: class ClassA { function hello () { echo "A hello\n"; } function __construct () { echo "A __construct\n"; } function __destruct () { echo "A __destruct\n"; } } class ClassB { function __construct () { echo "B __construct\n"; } function __destruct () { echo "B __destruct\n"; global $a; $a->hello(); } } $a = new ClassA; $b = new ClassB; As you can see the destructors get called in reverse order of the constructors: $ php ./test.php A __construct B __construct B __destruct A hello A __destruct If you switch the order by creating $b before $a you will get a fatal error: $ php ./test.php B __construct A __construct A __destruct B __destruct Fatal error: Call to a member function hello() on a non-object in test.php on line 18 This makes the use of destructors quite useless for tasks, which require proper dependency handling like freeing database statements before the database connection gets closed. Destructors can only be used for completely static and independent jobs. ---- Server IP: 212.124.37.9 Probable Submitter: 77.25.98.34 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php Edit -- https://master.php.net/note/edit/94894 Del: integrated -- https://master.php.net/note/delete/94894/integrated Del: useless -- https://master.php.net/note/delete/94894/useless Del: bad code -- https://master.php.net/note/delete/94894/bad+code Del: spam -- https://master.php.net/note/delete/94894/spam Del: non-english -- https://master.php.net/note/delete/94894/non-english Del: in docs -- https://master.php.net/note/delete/94894/in+docs Del: other reasons-- https://master.php.net/note/delete/94894 Reject -- https://master.php.net/note/reject/94894 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#163449) next »