3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class Ancestor { public $publicVar = "ancestor's public var\n"; static $staticVar = "ancestor's static var\n"; function publicMethod(){ echo "ancestor's publicMethod "; // is $this is set up if(isset($this)){ echo 'is in object context. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; }else{ echo "is in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; } } static function staticMethod(){ echo "ancestor's staticMethod "; // is $this is set up if(isset($this)){ echo 'in object context. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; }else{ echo "in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; } } function publicMethod2(){ echo "ancestor's publicMethod2 "; // is $this is set up if(isset($this)){ echo 'is in object context. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; }else{ echo "is in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; } } static function staticMethod2(){ echo "ancestor's staticMethod2 "; // is $this is set up if(isset($this)){ echo 'in object context. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; }else{ echo "in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; } } } class Descendant extends Ancestor { public $publicVar = "descendant's public var\n"; static $staticVar = "descendant's static var\n"; function publicMethod(){ echo "descendant's publicMethod "; // is $this is set up if(isset($this)){ echo 'in object context. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; }else{ echo "in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; } } static function staticMethod(){ echo "descendant's staticMethod "; // is $this is set up if(isset($this)){ echo 'in object context. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.". \n"; }else{ echo "in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; } } function publicMethod3(){ echo "descendant's publicMethod3 "; // is $this is set up if(isset($this)){ echo 'in object context. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; }else{ echo "in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; } } static function staticMethod3(){ echo "descendant's staticMethod3 "; // is $this is set up if(isset($this)){ echo 'in object context. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; }else{ echo "in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; } } function callToPropertiesFromPublicContext(){ echo $this->publicVar; // descendant's public var //echo $this->staticVar; // "" + notice: Undefined property: Descendant::$staticVar //echo $this::$publicVar; // fatal error: Access to undeclared static property: Descendant::$publicVar echo $this::$staticVar; // descendant's static var //echo Descendant::$publicVar; // fatal error: Access to undeclared static property: Descendant::$publicVar echo Descendant::$staticVar; // descendant's static var } static function callToPropertiesFromStaticContext(){ //echo Descendant::$publicVar; // fatal error: Access to undeclared static property: Descendant::$publicVar echo Descendant::$staticVar; // descendant's static var } function callMethodsFromPublicContext() { $this->publicMethod(); // descendant's publicMethod in object context $this->staticMethod(); // descendant's staticMethod in static context $this::publicMethod(); // descendant's publicMethod in object context $this::staticMethod(); // descendant's staticMethod in static context Descendant::publicMethod(); // descendant's publicMethod in object context Descendant::staticMethod(); // descendant's staticMethod in static context } static function callMethodsFromStaticContext() { Descendant::publicMethod(); // descendant's publicMethod in static context (deprecated) Descendant::staticMethod(); // descendant's staticMethod in static context } function callWithSelfFromPublicContext() { self::publicMethod(); self::publicMethod2(); self::publicMethod3(); self::staticMethod(); self::staticMethod2(); self::staticMethod3(); } static function callWithSelfFromStaticContext() { self::publicMethod(); self::publicMethod2(); self::publicMethod3(); self::staticMethod(); self::staticMethod2(); self::staticMethod3(); } } $ancestor = new Ancestor(); $descendant = new Descendant(); echo "\n1. From Outside Of An Instance/Class:\n"; echo "\n----- 1.1 access to properties -----\n"; echo $descendant->publicVar; // descendant's public var //echo $descendant->staticVar; // "" + notice: Undefined property: Descendant::$staticVar //echo $descendant::$publicVar; // fatal error: Access to undeclared static property: Descendant::$publicVar echo $descendant::$staticVar; // descendant's static var //echo Descendant::$publicVar; // fatal error: Access to undeclared static property echo Descendant::$staticVar; // descendant's static var echo "\n----- 1.2 access to methods -----\n"; $descendant->publicMethod(); // descendant's publicMethod in object context $descendant->staticMethod(); // descendant's staticMethod in static context $descendant::publicMethod(); // descendant's publicMethod in static context (deprecated) $descendant::staticMethod(); // descendant's staticMethod in static context Descendant::publicMethod(); // descendant's publicMethod in static context (deprecated) Descendant::staticMethod(); // descendant's staticMethod in static context echo "\n2. From Inside Of An Instance/Class:\n"; echo "\n----- 2.1 access to properties from public context -----\n"; $descendant->callToPropertiesFromPublicContext(); //echo $this->publicVar; // descendant's public var //echo $this->staticVar; // "" + notice: Undefined property: Descendant::$staticVar //echo $this::$publicVar; // fatal error: Access to undeclared static property: Descendant::$publicVar //echo $this::$staticVar; // descendant's static var // //echo Descendant::$publicVar; // fatal error: Access to undeclared static property: Descendant::$publicVar //echo Descendant::$staticVar; // descendant's static var echo "\n----- 2.2 access to properties from static context -----\n"; $descendant->callToPropertiesFromStaticContext(); //echo Descendant::$publicVar; // fatal error: Access to undeclared static property: Descendant::$publicVar //echo Descendant::$staticVar; // descendant's static var echo "\n----- 2.3 access to methods from public context -----\n"; $descendant->callMethodsFromPublicContext(); //$this->publicMethod(); // descendant's publicMethod in object context //$this->staticMethod(); // descendant's staticMethod in static context //$this::publicMethod(); // descendant's publicMethod in object context //$this::staticMethod(); // descendant's staticMethod in static context // //Descendant::publicMethod(); // descendant's publicMethod in object context //Descendant::staticMethod(); // descendant's staticMethod in static context echo "\n----- 2.4 access to methods from static context -----\n"; $descendant->callMethodsFromStaticContext(); //Descendant::publicMethod(); // descendant's publicMethod in static context (deprecated) //Descendant::staticMethod(); // descendant's staticMethod in static context echo "\n3. From Inside with 'forwarding calls' :\n"; echo "\n----- 3.1 self from public context -----\n"; $descendant->callWithSelfFromPublicContext(); echo "\n----- 3.2 self from static context -----\n"; Descendant::callWithSelfFromStaticContext(); // parent // static
based on FDdqI
Output for 7.0.0 - 7.2.0
1. From Outside Of An Instance/Class: ----- 1.1 access to properties ----- descendant's public var descendant's static var descendant's static var ----- 1.2 access to methods ----- descendant's publicMethod in object context. get_class($this):Descendant. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. Deprecated: Non-static method Descendant::publicMethod() should not be called statically in /in/q21oA on line 155 descendant's publicMethod in static context. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. Deprecated: Non-static method Descendant::publicMethod() should not be called statically in /in/q21oA on line 158 descendant's publicMethod in static context. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. 2. From Inside Of An Instance/Class: ----- 2.1 access to properties from public context ----- descendant's public var descendant's static var descendant's static var ----- 2.2 access to properties from static context ----- descendant's static var ----- 2.3 access to methods from public context ----- descendant's publicMethod in object context. get_class($this):Descendant. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. descendant's publicMethod in object context. get_class($this):Descendant. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. descendant's publicMethod in object context. get_class($this):Descendant. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. ----- 2.4 access to methods from static context ----- Deprecated: Non-static method Descendant::publicMethod() should not be called statically in /in/q21oA on line 116 descendant's publicMethod in static context. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. 3. From Inside with 'forwarding calls' : ----- 3.1 self from public context ----- descendant's publicMethod in object context. get_class($this):Descendant. get_class():Descendant. __CLASS__:Descendant. ancestor's publicMethod2 is in object context. get_class($this):Descendant. get_class():Ancestor. __CLASS__:Ancestor. descendant's publicMethod3 in object context. get_class($this):Descendant. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. ancestor's staticMethod2 in static context. get_class():Ancestor. __CLASS__:Ancestor. descendant's staticMethod3 in static context. get_class():Descendant. __CLASS__:Descendant. ----- 3.2 self from static context ----- Deprecated: Non-static method Descendant::publicMethod() should not be called statically in /in/q21oA on line 130 descendant's publicMethod in static context. get_class():Descendant. __CLASS__:Descendant. Deprecated: Non-static method Ancestor::publicMethod2() should not be called statically in /in/q21oA on line 131 ancestor's publicMethod2 is in static context. get_class():Ancestor. __CLASS__:Ancestor. Deprecated: Non-static method Descendant::publicMethod3() should not be called statically in /in/q21oA on line 132 descendant's publicMethod3 in static context. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. ancestor's staticMethod2 in static context. get_class():Ancestor. __CLASS__:Ancestor. descendant's staticMethod3 in static context. get_class():Descendant. __CLASS__:Descendant.
Output for 5.4.0 - 5.6.28
1. From Outside Of An Instance/Class: ----- 1.1 access to properties ----- descendant's public var descendant's static var descendant's static var ----- 1.2 access to methods ----- descendant's publicMethod in object context. get_class($this):Descendant. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. Strict Standards: Non-static method Descendant::publicMethod() should not be called statically in /in/q21oA on line 155 descendant's publicMethod in static context. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. Strict Standards: Non-static method Descendant::publicMethod() should not be called statically in /in/q21oA on line 158 descendant's publicMethod in static context. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. 2. From Inside Of An Instance/Class: ----- 2.1 access to properties from public context ----- descendant's public var descendant's static var descendant's static var ----- 2.2 access to properties from static context ----- descendant's static var ----- 2.3 access to methods from public context ----- descendant's publicMethod in object context. get_class($this):Descendant. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. descendant's publicMethod in object context. get_class($this):Descendant. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. descendant's publicMethod in object context. get_class($this):Descendant. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. ----- 2.4 access to methods from static context ----- Strict Standards: Non-static method Descendant::publicMethod() should not be called statically in /in/q21oA on line 116 descendant's publicMethod in static context. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. 3. From Inside with 'forwarding calls' : ----- 3.1 self from public context ----- descendant's publicMethod in object context. get_class($this):Descendant. get_class():Descendant. __CLASS__:Descendant. ancestor's publicMethod2 is in object context. get_class($this):Descendant. get_class():Ancestor. __CLASS__:Ancestor. descendant's publicMethod3 in object context. get_class($this):Descendant. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. ancestor's staticMethod2 in static context. get_class():Ancestor. __CLASS__:Ancestor. descendant's staticMethod3 in static context. get_class():Descendant. __CLASS__:Descendant. ----- 3.2 self from static context ----- Strict Standards: Non-static method Descendant::publicMethod() should not be called statically in /in/q21oA on line 130 descendant's publicMethod in static context. get_class():Descendant. __CLASS__:Descendant. Strict Standards: Non-static method Ancestor::publicMethod2() should not be called statically in /in/q21oA on line 131 ancestor's publicMethod2 is in static context. get_class():Ancestor. __CLASS__:Ancestor. Strict Standards: Non-static method Descendant::publicMethod3() should not be called statically in /in/q21oA on line 132 descendant's publicMethod3 in static context. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. ancestor's staticMethod2 in static context. get_class():Ancestor. __CLASS__:Ancestor. descendant's staticMethod3 in static context. get_class():Descendant. __CLASS__:Descendant.