NYCPHP Meetup

[nycphp-talk] PHP 4 Constructor Weirdness

Jay Sheth jay_nyphp2 at fastmail.fm
Mon Jan 8 12:24:37 EST 2007


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?

 Ordinarily, it should have the same name as the class. I think the
 'parent::xyz' call is somehow matched to the 'extends xyz' declaration,
 and the constructor is found. Is this a bug or a feature?

Regards, 
- Jay




More information about the talk mailing list