NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP 4 Constructor Weirdness

Chris Shiflett shiflett at php.net
Mon Jan 8 13:03:15 EST 2007


Jay Sheth wrote:
> 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?

False assumptions form the basis of many programming errors. I'd suggest
that you first verify your assumptions. Are you sure the def() method
being called? If it is, that's insane, but I bet it's not. :-)

Here's a hint. You're extending a class that has a constructor. You
don't provide a constructor in the definition of the child class...

Hope that helps.

Chris

-- 
Chris Shiflett
http://shiflett.org/



More information about the talk mailing list