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 "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"; } } } $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 Descendant::$publicVar; // fatal error: Access to undeclared static property echo Descendant::$staticVar; // descendant's static var // access to methods $descendant->publicMethod(); // descendant's publicMethod in object context $descendant->staticMethod(); // descendant's staticMethod in static context $descendant::staticMethod(); // descendant's staticMethod in static context $descendant::publicMethod(); // descendant's publicMethod in static context Descendant::publicMethod(); // Descendant::staticMethod(); // //Ancestor::publicMethod();
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
descendant's public var descendant's static var descendant's static var descendant's publicMethod in object context descendant's staticMethod in static context descendant's staticMethod in static context Fatal error: Uncaught Error: Non-static method Descendant::publicMethod() cannot be called statically in /in/uqDYj:68 Stack trace: #0 {main} thrown in /in/uqDYj on line 68
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
descendant's public var descendant's static var descendant's static var descendant's publicMethod in object context descendant's staticMethod in static context descendant's staticMethod in static context Deprecated: Non-static method Descendant::publicMethod() should not be called statically in /in/uqDYj on line 68 descendant's publicMethod in static context Deprecated: Non-static method Descendant::publicMethod() should not be called statically in /in/uqDYj on line 70 descendant's publicMethod in static context descendant's staticMethod in static context
Output for 7.3.32 - 7.3.33
descendant's public var descendant's static var descendant's static var descendant's publicMethod in object context descendant's staticMethod in static context descendant's staticMethod in static context descendant's publicMethod in static context descendant's publicMethod in static context descendant's staticMethod in static context
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
descendant's public var descendant's static var descendant's static var descendant's publicMethod in object context descendant's staticMethod in static context descendant's staticMethod in static context Strict Standards: Non-static method Descendant::publicMethod() should not be called statically in /in/uqDYj on line 68 descendant's publicMethod in static context Strict Standards: Non-static method Descendant::publicMethod() should not be called statically in /in/uqDYj on line 70 descendant's publicMethod in static context descendant's staticMethod in static context

preferences:
273.1 ms | 403 KiB | 374 Q