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"; } } function publicMethod2(){ echo "ancestor's publicMethod2 "; // is $this is set up if(isset($this)){ echo "is in object context\n"; }else{ echo "is in static context\n"; } } static function staticMethod2(){ echo "ancestor's staticMethod2 "; // 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 publicMethod3(){ echo "descendant's publicMethod3 "; // is $this is set up if(isset($this)){ echo "in object context\n"; }else{ echo "in static context\n"; } } static function staticMethod3(){ echo "descendant's staticMethod3 "; // 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; // 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----- 2.4 self from public context -----\n"; $descendant->callWithSelfFromPublicContext(); echo "\n----- 2.4 self from static context -----\n"; Descendant::callWithSelfFromStaticContext(); // parent // static
based on Qtpi5
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/n7Gtu on line 155 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/n7Gtu on line 158 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 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 descendant's staticMethod in static context descendant's publicMethod in object context descendant's staticMethod in static context descendant's publicMethod in object context descendant's staticMethod in static context ----- 2.4 access to methods from static context ----- Deprecated: Non-static method Descendant::publicMethod() should not be called statically in /in/n7Gtu on line 116 descendant's publicMethod in static context descendant's staticMethod in static context 3. From Inside with 'forwarding calls' : ----- 2.4 self from public context ----- descendant's publicMethod in object context ancestor's publicMethod2 is in object context descendant's publicMethod3 in object context descendant's staticMethod in static context ancestor's staticMethod2 in static context descendant's staticMethod3 in static context ----- 2.4 self from static context ----- Deprecated: Non-static method Descendant::publicMethod() should not be called statically in /in/n7Gtu on line 130 descendant's publicMethod in static context Deprecated: Non-static method Ancestor::publicMethod2() should not be called statically in /in/n7Gtu on line 131 ancestor's publicMethod2 is in static context Deprecated: Non-static method Descendant::publicMethod3() should not be called statically in /in/n7Gtu on line 132 descendant's publicMethod3 in static context descendant's staticMethod in static context ancestor's staticMethod2 in static context descendant's staticMethod3 in static context
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/n7Gtu on line 155 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/n7Gtu on line 158 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 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 descendant's staticMethod in static context descendant's publicMethod in object context descendant's staticMethod in static context descendant's publicMethod in object context descendant's staticMethod in static context ----- 2.4 access to methods from static context ----- Strict Standards: Non-static method Descendant::publicMethod() should not be called statically in /in/n7Gtu on line 116 descendant's publicMethod in static context descendant's staticMethod in static context 3. From Inside with 'forwarding calls' : ----- 2.4 self from public context ----- descendant's publicMethod in object context ancestor's publicMethod2 is in object context descendant's publicMethod3 in object context descendant's staticMethod in static context ancestor's staticMethod2 in static context descendant's staticMethod3 in static context ----- 2.4 self from static context ----- Strict Standards: Non-static method Descendant::publicMethod() should not be called statically in /in/n7Gtu on line 130 descendant's publicMethod in static context Strict Standards: Non-static method Ancestor::publicMethod2() should not be called statically in /in/n7Gtu on line 131 ancestor's publicMethod2 is in static context Strict Standards: Non-static method Descendant::publicMethod3() should not be called statically in /in/n7Gtu on line 132 descendant's publicMethod3 in static context descendant's staticMethod in static context ancestor's staticMethod2 in static context descendant's staticMethod3 in static context