@ 2015-09-24T20:33:38Z <?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
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.4 , 8.3.6 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
Fatal error: Uncaught Error: Object of class Descendant could not be converted to string in /in/NuZ7m:54
Stack trace:
#0 /in/NuZ7m(153): Descendant->publicMethod()
#1 {main}
thrown in /in/NuZ7m on line 54
Process exited with code 255 . Output for 8.3.5 Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 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
Fatal error: Uncaught Error: Object of class Descendant could not be converted to string in /in/NuZ7m:54
Stack trace:
#0 /in/NuZ7m(153): Descendant->publicMethod()
#1 {main}
thrown in /in/NuZ7m on line 54
Process exited with code 255 . Output for 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 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
Recoverable fatal error: Object of class Descendant could not be converted to string in /in/NuZ7m on line 54
Process exited with code 255 . Output for 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.28 , 7.0.0 - 7.0.20 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
Catchable fatal error: Object of class Descendant could not be converted to string in /in/NuZ7m on line 54
Process exited with code 255 . preferences:dark mode live preview
226.68 ms | 402 KiB | 316 Q