[nycphp-talk] PHP 4 Constructor Weirdness
Scott Mattocks
scott at crisscott.com
Mon Jan 8 12:35:09 EST 2007
Jay Sheth wrote:
> Hi NYPHPers,
>
> Here's a question:
> What would you expect to be outputted below (using PHP 4)?
>
> <?php
> class xyz{
> function xyz($id){
> $this->id = $id;
> }
> }
>
> class abc extends xyz{
> function def($id){
> parent::xyz($id);
> }
> }
>
> $test = new abc(43);
> echo $test->id;
> ?>
>
> If you answered 'nothing', you're wrong. It outputs '43'. I'm wondering
> how PHP find that def() is the constructor when instantiating the abc
> class?
>
It doesn't call def() at all. If you don't provide a constructor for a
class, PHP calls the parent class's constructor. In this case xyz(). If
class xyz didn't have a constructor, then you should expect nothing to
be returned. Try putting an echo into def() to verify what is and is not
being called.
Scott Mattocks
More information about the talk
mailing list