destructor being called by subclass
Posted
by
zero
on Stack Overflow
See other posts from Stack Overflow
or by zero
Published on 2012-12-03T16:54:47Z
Indexed on
2012/12/03
17:03 UTC
Read the original article
Hit count: 145
php
|destructor
I'm currently learning more about php objects and constructors/destructors, but i've noticed in my code that the parent class's destructor is being called twice, I thought it was because i was extending the first class to my second class and that the second class was calling it, but this is what the php docs say about that:
Like constructors, parent destructors will not be called implicitly by the engine. In order to run a parent destructor, one would have to explicitly call parent::__destruct() in the destructor body.
so if it is not being called by the subclass then is it because by extended the first class that i've made a reference to the parent class making it call itself twice or I'm I way off base here?
the code:
<?php
class test{
public $test1 = "this is a test of a pulic property";
private $test2 = "this is a test of a private property";
protected $test3 = "this is a test of a protected property";
const hello = 900000;
function __construct($h){
//echo 'this is the constructor test '.$h;
}
function x($x2){
echo ' this is fn x'.$x2;
}
function y(){
print "this is fn y";
}
}
$obj = new test("this is an \"arg\" sent to instance of test");
class hey extends test{
function hey(){
$this->x('<br>from the host with the most');
echo ' <br>from hey class'.$this->test3;
}
}
$obj2 = new hey();
echo $obj2::hello;
?>
© Stack Overflow or respective owner