NYCPHP Meetup

NYPHP.org

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

Andrew Yochum andrew at digitalpulp.com
Wed Jun 2 11:05:58 EDT 2004


On Wed, Jun 02, 2004 at 10:48:04AM -0400, Phillip Powell wrote:
> [PHP]
> <?php
> 
>  class SuperClass {
> 
>     var $mySuperClassVar;
> 
>     function SuperClass($myVar) {
>       $this->mySuperClassVar = $myVar;
>       echo "super class var = $myVar<p>";
>     }
> 
>  }
> 
>  class SubClass extends SuperClass {
> 
>    var $mySubClassVar;
> 
>    function SubClass($myVar) {
>     // super('hello world?')???
>     $this->mySubClassVar = $myVar;
>     echo "sub class var = $myVar<p>";
>    }
> 
>  }
> 
>  $obj =& new SubClass('what is up with your bad self');
> 
> ?>
> [/PHP]
> 
> [output]
>  hello world
>  what is up with your bad self
> [/output]
> 
> I am interested in finding out if PHP has an equivalent to the Java 
> "super" keyword that evokes methods or constructor of the class' parent 
> class.  I can't find anything online on this and hoped maybe one of you 
> guys came up with a nice workaround for this in PHP 4.3.2+ that I could 
> learn.  Or point me in the right, open-source, direction for me to 
> figure this out.

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.

HTH,
Andrew



More information about the talk mailing list