<?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; // descendant's static var
echo $descendant->staticVar; // descendant's public var
echo $descendant::$publicVar; // descendant's static var
//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
//$ancestor->
//Ancestor::publicMethod();
descendant's public var
descendant's static var
Notice: Accessing static property Descendant::$staticVar as non static in /in/4SYh6 on line 58
Warning: Undefined property: Descendant::$staticVar in /in/4SYh6 on line 58
Fatal error: Uncaught Error: Access to undeclared static property Descendant::$publicVar in /in/4SYh6:59
Stack trace:
#0 {main}
thrown in /in/4SYh6 on line 59
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
descendant's public var
descendant's static var
Notice: Accessing static property Descendant::$staticVar as non static in /in/4SYh6 on line 58
Warning: Undefined property: Descendant::$staticVar in /in/4SYh6 on line 58
Fatal error: Uncaught Error: Access to undeclared static property Descendant::$publicVar in /in/4SYh6:59
Stack trace:
#0 {main}
thrown in /in/4SYh6 on line 59
Process exited with code 255.
descendant's public var
descendant's static var
Notice: Accessing static property Descendant::$staticVar as non static in /in/4SYh6 on line 58
Notice: Undefined property: Descendant::$staticVar in /in/4SYh6 on line 58
Fatal error: Uncaught Error: Access to undeclared static property: Descendant::$publicVar in /in/4SYh6:59
Stack trace:
#0 {main}
thrown in /in/4SYh6 on line 59
Process exited with code 255.
Output for 7.3.32 - 7.3.33
descendant's public var
descendant's static var
Fatal error: Uncaught Error: Access to undeclared static property: Descendant::$publicVar in /in/4SYh6:59
Stack trace:
#0 {main}
thrown in /in/4SYh6 on line 59
Process exited with code 255.
descendant's public var
descendant's static var
Strict Standards: Accessing static property Descendant::$staticVar as non static in /in/4SYh6 on line 58
Notice: Undefined property: Descendant::$staticVar in /in/4SYh6 on line 58
Fatal error: Access to undeclared static property: Descendant::$publicVar in /in/4SYh6 on line 59
Process exited with code 255.