note 126914 added to language.oop5.decon
| From: | mmulej at gmail dot com | Date: | Thu, 10 Mar 2022 16:56:42 +0000 |
| Subject: | note 126914 added to language.oop5.decon | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217705@lists.php.net to get a copy of this message | ||
*<Double post> I can't edit my previous note to elaborate on modifiers. Please excuse
me.*
If both parent and child classes have a method with the same name defined, and it is called in
parent's constructor, using
parent::__construct() will call the method in the
child.
<?php
class A {
public function __construct() {
$this->method();
}
public function method() {
echo 'A' . PHP_EOL;
}
}
class B extends A {
public function __construct() {
parent::__construct();
}
}
class C extends A {
public function __construct() {
parent::__construct();
}
public function method() {
echo 'C' . PHP_EOL;
}
}
$b = new B; // A
$c = new C; // C
?>
In this example both A::method and C::method are public.
You may change A::method to protected, and C::method to protected or public and it will still work
the same.
If however you set A::method as private, it doesn't matter whether C::method is private,
protected or public. Both $b and $c will echo 'A'.
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.135 (proxied: 89.142.234.171)
----
Manual Page -- https://php.net/manual/en/language.oop5.decon.php
Edit -- https://main.php.net/note/edit/126914
Del: integrated -- https://main.php.net/note/delete/126914/integrated
Del: useless -- https://main.php.net/note/delete/126914/useless
Del: bad code -- https://main.php.net/note/delete/126914/bad+code
Del: spam -- https://main.php.net/note/delete/126914/spam
Del: non-english -- https://main.php.net/note/delete/126914/non-english
Del: in docs -- https://main.php.net/note/delete/126914/in+docs
Del: other reasons-- https://main.php.net/note/delete/126914
Reject -- https://main.php.net/note/reject/126914
Search -- https://main.php.net/manage/user-notes.php