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\n"; }else{ echo "is in static context\n"; } } static function staticMethod(){ echo "ancestor's staticMethod "; // is $this is set up if(isset($this)){ echo "in object context\n"; }else{ echo "in static context\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\n"; }else{ echo "in static context\n"; } } static function staticMethod(){ echo "descendant's staticMethod "; // is $this is set up if(isset($this)){ echo "in object context\n"; }else{ echo "in static context\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; echo Descendant::$publicVar; echo Descendant::$staticVar; } static function callToPropertiesFromStaticContext(){ echo Descendant::$publicVar; echo Descendant::$staticVar; } } $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 $descendant::staticMethod(); // descendant's staticMethod in static context Descendant::publicMethod(); // descendant's publicMethod in static context 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 "\n----- 2.1 access to properties from static context -----\n"; $descendant->callToPropertiesFromStaticContext();
based on R2hos
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 descendant's staticMethod in static context Deprecated: Non-static method Descendant::publicMethod() should not be called statically in /in/fHGHA on line 80 descendant's publicMethod in static context descendant's staticMethod in static context Deprecated: Non-static method Descendant::publicMethod() should not be called statically in /in/fHGHA on line 83 descendant's publicMethod in static context descendant's staticMethod in static context 2. From Inside Of An Instance/Class: ----- 2.1 access to properties from public context ----- descendant's public var descendant's static var Fatal error: Uncaught Error: Access to undeclared static property: Descendant::$publicVar in /in/fHGHA:55 Stack trace: #0 /in/fHGHA(88): Descendant->callToPropertiesFromPublicContext() #1 {main} thrown in /in/fHGHA on line 55
Process exited with code 255.
Output for 5.3.18 - 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 descendant's staticMethod in static context Strict Standards: Non-static method Descendant::publicMethod() should not be called statically in /in/fHGHA on line 80 descendant's publicMethod in static context descendant's staticMethod in static context Strict Standards: Non-static method Descendant::publicMethod() should not be called statically in /in/fHGHA on line 83 descendant's publicMethod in static context descendant's staticMethod in static context 2. From Inside Of An Instance/Class: ----- 2.1 access to properties from public context ----- descendant's public var descendant's static var Fatal error: Access to undeclared static property: Descendant::$publicVar in /in/fHGHA on line 55
Process exited with code 255.