note 93097 modified in language.oop5.decon by danbrown
| From: | danbrown@php.net | Date: | Sun, 23 Aug 2009 17:06:51 +0000 |
| Subject: | note 93097 modified in language.oop5.decon by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-159811@lists.php.net to get a copy of this message | ||
It's usefull to note that parent::__construct() is executed like a standalone function called
into the new one, and not like a first part of the code. So "return" or "die()"
don't have effect on following code. If you want to avoid the execution of the child
constructor you have to pass some condition from the parent. Example:
<?php
class X {
function X(){
if($_POST['break']=='yes'){return;}
}
}
class Y extends X{
function Y(){
parent::__construct();
print("Y_constructor_finished");
}
}
$_POST['break']=='yes';
$new_Y=new Y(); // will print "Y_constructor_finished"
Solution:
class X {
function X(){
if($_POST['break']=='yes'){$this->break='yes';return;}
}
}
class Y extends X{
function Y(){
parent::__construct();
if($this->break=='yes'){return;}
print("Y_constructor_finished");
}
}
$_POST['break']='yes';
$new_Y=new Y(); // will print nothing
?>
--was--
It's usefull to note that parent::__construct() is executed like a standalone function called
into the new one, and not like a first part of the code. So "return" or "die()"
don't have effect on following code. If you want to avoid the execution of the child
constructor you have to pass some condition from the parent. Example:
class X {
function X(){
if($_POST['break']=='yes'){return;}
}
}
class Y extends X{
function Y(){
parent::__construct();
print("Y_constructor_finished");
}
}
$_POST['break']=='yes';
$new_Y=new Y(); // will print "Y_constructor_finished"
Solution:
class X {
function X(){
if($_POST['break']=='yes'){$this->break='yes';return;}
}
}
class Y extends X{
function Y(){
parent::__construct();
if($this->break=='yes'){return;}
print("Y_constructor_finished");
}
}
$_POST['break']='yes';
$new_Y=new Y(); // will print nothing
http://php.net/manual/en/language.oop5.decon.php