NYCPHP Meetup

NYPHP.org

[nycphp-talk] Does PHP have an equivalent to super()?

Dan Cech dcech at phpwerx.net
Wed Jun 2 11:21:14 EDT 2004


Andrew Yochum wrote:
> Its not quite the same as "super" because its not generic but this works:
>   class SubClass extends SuperClass {
> 
>     var $mySubClassVar;
>  
>     function SubClass($myVar) {
>      parent::SuperClass('hello world?');
>      $this->mySubClassVar = $myVar;
>      echo "sub class var = $myVar<p>";
>     }
>  
>   }
> 
> PHP 5 offers unified constructors using the __construct() method, which will
> allow you to generically call parent::__construct() and get the constructor in
> every case.

One workaround for constructor names in php4 is as follows:

class SubClass extends SuperClass {
   var $mySubClassVar;

   function SubClass($myVar) {
     $constructor = get_parent_class($this);
     parent::$constructor('hello world?');
     $this->mySubClassVar = $myVar;
     echo "sub class var = $myVar<p>";
   }
}

This should work in every case.

Dan




More information about the talk mailing list