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. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; }else{ echo "is in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; } } static function staticMethod(){ echo "ancestor's staticMethod "; // is $this is set up if(isset($this)){ echo 'in object context. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; }else{ echo "in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; } } function publicMethod2(){ echo "ancestor's publicMethod2 "; // is $this is set up if(isset($this)){ echo 'is in object context. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; }else{ echo "is in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; } } static function staticMethod2(){ echo "ancestor's staticMethod2 "; // is $this is set up if(isset($this)){ echo 'in object context. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; }else{ echo "in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\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. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; }else{ echo "in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; } } static function staticMethod(){ echo "descendant's staticMethod "; // is $this is set up if(isset($this)){ echo 'in object context. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.". \n"; }else{ echo "in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; } } function publicMethod3(){ echo "descendant's publicMethod3 "; // is $this is set up if(isset($this)){ echo 'in object context. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; }else{ echo "in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; } } static function staticMethod3(){ echo "descendant's staticMethod3 "; // is $this is set up if(isset($this)){ echo 'in object context. get_class($this):'.get_class($this).". get_class():".get_class().". __CLASS__:".__CLASS__.".\n"; }else{ echo "in static context. get_class():".get_class().". __CLASS__:".__CLASS__.".\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----- 3.1 self from public context -----\n"; $descendant->callWithSelfFromPublicContext(); echo "\n----- 3.2 self from static context -----\n"; Descendant::callWithSelfFromStaticContext(); // parent // static
Output for git.master, git.master_jit, rfc.property-hooks
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. get_class($this):Descendant. get_class():Descendant. __CLASS__:Descendant. descendant's staticMethod in static context. get_class():Descendant. __CLASS__:Descendant. Fatal error: Uncaught Error: Non-static method Descendant::publicMethod() cannot be called statically in /in/q21oA:155 Stack trace: #0 {main} thrown in /in/q21oA on line 155
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
44.1 ms | 401 KiB | 8 Q