note 124565 added to language.oop5.inheritance
| From: | niemans at pbsolo dot nl | Date: | Sat, 04 Jan 2020 13:39:30 +0000 |
| Subject: | note 124565 added to language.oop5.inheritance | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-213729@lists.php.net to get a copy of this message | ||
Inheritance works at create time, i.e. using the keyword 'new'. Static properties confused
my understanding, so in order tho show the effect of visibility to inherintence I've created a
simple demo script along with some set and get magic:
<?php
class A {
private $a = 'private';
protected $b = 'protected';
public $c = 'public';
static $d = 'static';
public function __construct()
{
$this->e = 'constructed';
}
public function __set($property, $value)
{
echo ' set ' . $property . '=' . $value;
$this->$property=$value;
}
public function __get($property)
{
echo ' get ' . $property;
$this->$property = 'dynamic'; // invokes __set() !!
return $this->$property;
}
}
class B extends A
{
public function constructMe()
{
$this->e = 'constructed2';
}
}
class C extends B
{
public function __construct()
{
parent::constructMe();
}
}
echo " \n";
$a = new A();
$b = new B();
echo " \n";
echo ' B:c='.$b->c;
echo " \n";
echo ' B:d=' .$b->d;
echo " \n";
$c = new C();
echo " \n";
print_r($a);
print_r($b);
print_r($c);
print_r(A::$d);
print_r(B::$d);
print_r(C::$d);
echo 'A class: ';
$R = new reflectionclass('A');
print_r($R->getdefaultproperties());
print_r($R->getstaticproperties());
echo 'B class: ';
$R = new reflectionclass('B');
print_r($R->getdefaultproperties());
print_r($R->getstaticproperties());
?>
This outputs:
set e=constructed
B:c=public
get d set d=dynamic B:d=dynamic
set e=constructed2
A Object
(
[a:A:private] => private
[b:protected] => protected
[c] => public
[e] => constructed
)
B Object
(
[a:A:private] => private
[b:protected] => protected
[c] => public
[d] => dynamic
)
C Object
(
[a:A:private] => private
[b:protected] => protected
[c] => public
[e] => constructed2
)
staticstaticstaticA class: Array
(
[d] => static
[a] => private
[b] => protected
[c] => public
)
Array
(
[d] => static
)
B class: Array
(
[d] => static
[b] => protected
[c] => public
)
Array
(
[d] => static
)
This shows how private variables ($a) are inherited, how static variables ($d) are inherited (by the
class, not by the object) and that changing or adding variables in the parent ($e, $d) are not
inherited by the child.
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.195 (proxied: 92.209.33.70)
----
Manual Page -- http://php.net/manual/en/language.oop5.inheritance.php
Edit -- https://master.php.net/note/edit/124565
Del: integrated -- https://master.php.net/note/delete/124565/integrated
Del: useless -- https://master.php.net/note/delete/124565/useless
Del: bad code -- https://master.php.net/note/delete/124565/bad+code
Del: spam -- https://master.php.net/note/delete/124565/spam
Del: non-english -- https://master.php.net/note/delete/124565/non-english
Del: in docs -- https://master.php.net/note/delete/124565/in+docs
Del: other reasons-- https://master.php.net/note/delete/124565
Reject -- https://master.php.net/note/reject/124565
Search -- https://master.php.net/manage/user-notes.php