3v4l.org

run code in 300+ PHP versions simultaneously
<?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; // "" 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() //Ancestor::publicMethod();
Output for 5.4.1 - 5.4.45, 5.5.24 - 5.5.29, 5.6.8 - 5.6.13
Parse error: syntax error, unexpected end of file in /in/is0qc on line 69
Process exited with code 255.

preferences:
161.84 ms | 1395 KiB | 63 Q