Page 1 of 1

Constructor Clarification

Posted: Mon Jul 30, 2007 12:01 pm
by TheMoose
Just need some clarification of the following from the PHP Manual:
"Note: Parent constructors are not called implicitly if the child class defines a constructor"

My perception is that if I do not define a constructor in the child, then the parent's will be called on a new instance of the child class. Is this assumption correct? If I define one in the child class, then I would have to call parent::__construct() as noted.

Posted: Mon Jul 30, 2007 12:04 pm
by John Cartwright
Right.

Posted: Mon Jul 30, 2007 12:05 pm
by TheMoose
Jcart wrote:Right.
Good, thanks :)

Posted: Mon Jul 30, 2007 5:13 pm
by nathanr
*ponders*

so if we run these two lines in succession:

Code: Select all

$this->__destruct();
$this->__constuct();
and we only have a _cconstruct in the class which is being extended, then have we just "reset" our parent class, or created an error? (I should really just type a few lines and test this myself..

Posted: Mon Jul 30, 2007 5:18 pm
by feyd
You are not supposed to call them directly, ever, unless a child is calling the parent's while in that method. Calling them directly will do nothing special. It's exactly as if you called any other method.

Posted: Mon Jul 30, 2007 5:24 pm
by nathanr
agreed, simply a what would happen if.. question - going to do it just now and see what the results are

Posted: Mon Jul 30, 2007 5:48 pm
by nathanr
yeah feyd was right, didn't do anything special.. HOWEVER found something I never realised before..

Code: Select all

<?

class egg {
	public $somevar;
	
	public function __construct() {
		$this->somevar = 'egg __construct string';
	}
	
	public function __destruct() {
		$this->somevar = 'egg __destruct string';
		print_r("\n".$this->somevar."\n\n");
		$this->__construct();              #REFERENCE X
	}
}

class shell extends egg {
	
	public function __construct() {
		$this->somevar = 'shell __construct string';
	}
}

$eggshell = new shell();
print_r($eggshell);
$eggshell->__destruct();
print_r($eggshell);

?>
you'll see a line called REFERENCE X
now I would have expected this call to __construct() from within __destruct() to call the __construct of egg, however nope.. it calls the __construct of shell.

here's the output returned

Code: Select all

shell Object
(
    [somevar] => shell __construct string
)

egg __destruct string

shell Object
(
    [somevar] => shell __construct string
)
so the parent is calling a function from the class that extends it...

Posted: Mon Jul 30, 2007 6:24 pm
by superdezign
nathanr wrote:so the parent is calling a function from the class that extends it...
No it is not. When a class is extended, it's non-private functions and data become a part of the new class unless overwritten. So, $this->__construct() is called from the copy of __destruct() that belongs to shell, not from egg.

Try making __destruct() private, then call it from the shell object.

Posted: Mon Jul 30, 2007 6:27 pm
by nathanr
yep.. I need a holiday