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 "this is ancestor's public method\n"; // is $this is set up if(isset($this)){ echo "publicMethod is in object context\n"; }else{ echo "publicMethod is in static context\n"; } } static function staticMethod(){ echo "this is ancestor's static method\n"; // is $this is set up if(isset($this)){ echo "staticMethod is in object context\n"; }else{ echo "staticMethod is 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 "this is descendant's public method\n"; // is $this is set up if(isset($this)){ echo "publicMethod is in object context\n"; }else{ echo "publicMethod is in static context\n"; } } static function staticMethod(){ echo "this is descendant's static method\n"; // is $this is set up if(isset($this)){ echo "staticMethod is in object context\n"; }else{ echo "staticMethod is in static context\n"; } } } $ancestor = new Ancestor(); $descendant = new Descendant(); // From Outside Of An Instance/Class: // access to properties echo $descendant->publicVar; // descendant's public var echo $descendant->staticVar; // "" + notice: Undefined property: Descendant::$staticVar echo $descendant::$staticVar; // descendant's static var //echo $descendant::$publicVar; // fatal error: Access to undeclared static property: Descendant::$publicVar //echo Ancestor::$publicVar; // fatal error: Access to undeclared static property echo Ancestor::$staticVar; //ancestor's static var //echo Descendant::$publicVar; // fatal error: Access to undeclared static property echo Descendant::$staticVar; // descendant's static var // access to methods $descendant->publicMethod(); $descendant->staticMethod(); $descendant::staticMethod(); $descendant::publicMethod(); //Ancestor::publicMethod();
based on askQN
Output for 7.0.0 - 7.2.0
descendant's public var Notice: Accessing static property Descendant::$staticVar as non static in /in/l8Gj2 on line 57 Notice: Undefined property: Descendant::$staticVar in /in/l8Gj2 on line 57 descendant's static var ancestor's static var descendant's static var this is descendant's public method publicMethod is in object context this is descendant's static method staticMethod is in static context this is descendant's static method staticMethod is in static context Deprecated: Non-static method Descendant::publicMethod() should not be called statically in /in/l8Gj2 on line 70 this is descendant's public method publicMethod is in static context
Output for 5.4.0 - 5.6.28
descendant's public var Strict Standards: Accessing static property Descendant::$staticVar as non static in /in/l8Gj2 on line 57 Notice: Undefined property: Descendant::$staticVar in /in/l8Gj2 on line 57 descendant's static var ancestor's static var descendant's static var this is descendant's public method publicMethod is in object context this is descendant's static method staticMethod is in static context this is descendant's static method staticMethod is in static context Strict Standards: Non-static method Descendant::publicMethod() should not be called statically in /in/l8Gj2 on line 70 this is descendant's public method publicMethod is in static context