note 93387 added to language.oop.constructor

From: Date: Mon, 07 Sep 2009 13:34:48 +0000
Subject: note 93387 added to language.oop.constructor
Groups: php.notes 
Request: Send a blank email to php-notes+get-160278@lists.php.net to get a copy of this message
I previously used C++ and noted, that PHP does not invoke parent class destructor automatically, but must be called explicitly from derived __destruct(). To illustrate, the next code <?php /* * test object __destruct bevahior */ class TestDtrA { public function __construct() { echo __CLASS__ . " created\n"; } public function __destruct() { echo __CLASS__ . " destructor called\n"; } } class TestDtrB extends TestDtrA { public function __construct() { echo __CLASS__ . " created\n"; } public function __destruct() { // parent::__destruct(); <<explicit parent call echo __CLASS__ . " destructor called\n"; } } function foo() { echo __FUNCTION__ . ": in\n"; $instance = new TestDtrB(); echo __FUNCTION__ . ": out\n"; } function bar() { echo __FUNCTION__ . ": in\n"; echo __FUNCTION__ . ": out\n"; } foo(); bar(); ?> will output foo: in TestDtrB created foo: out TestDtrB destructor called bar: in bar: out (PHP 5.2.6) ---- Server IP: 209.41.74.194 Probable Submitter: 195.238.92.73 ---- Manual Page -- http://www.php.net/manual/en/language.oop.constructor.php Edit -- https://master.php.net/note/edit/93387 Del: integrated -- https://master.php.net/note/delete/93387/integrated Del: useless -- https://master.php.net/note/delete/93387/useless Del: bad code -- https://master.php.net/note/delete/93387/bad+code Del: spam -- https://master.php.net/note/delete/93387/spam Del: non-english -- https://master.php.net/note/delete/93387/non-english Del: in docs -- https://master.php.net/note/delete/93387/in+docs Del: other reasons-- https://master.php.net/note/delete/93387 Reject -- https://master.php.net/note/reject/93387 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#160278) next »