note 72232 added to keyword.extends
| From: | TimMaynard at osu1 dot php dot net | Date: | Mon, 08 Jan 2007 21:55:56 +0000 |
| Subject: | note 72232 added to keyword.extends | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121463@lists.php.net to get a copy of this message | ||
multiple inheritance has been a constant concern for me (and several others) and i found that if you
declare the class's members and methods as protected, several benefits arise:
- the members and methods are only accessible internally from the current and inherited class's
methods.
- any non-overloaded members and methods of a parent are accessible to the entire depth of children
- overloading of members and methods are normal for any depth of children
+ prevents name clashing because it tells PHP which order to overload identically named members and
methods
<?php
class A {
protected $a = 'im a';
protected function getMe()
{
return $this->a."\n";
}
public function __construct()
{
echo $this->getMe();
}
}
class B extends A {
protected $b = 'im b';
protected function getMe()
{
return $this->b.' ... '.$this->a."\n";
}
public function __construct()
{
echo $this->getMe();
}
}
class C extends B {
protected $c = 'im c';
protected function getMe()
{
return $this->c.' ... '.$this->b.' ... '.$this->a."\n";
}
public function __construct()
{
echo $this->getMe();
}
}
class D extends C {
protected $d = 'im d';
protected function getMe()
{
return $this->d.' ... '.$this->c.' ... '.$this->b.' ...
'.$this->a."\n";
}
public function __construct()
{
echo $this->getMe();
}
}
$mya = new A;
$myb = new B;
$myc = new C;
$myc = new D;
?>
this prints the following w/o error:
im a
im b ... im a
im c ... im b ... im a
im d ... im c ... im b ... im a
in summary, the "protected" declaration keeps whats private private and whats supposed to
be public public and allows similar functionality of multiple inheritance -- though not as easily as
C++'s , (comma) but still present for those in need.
----
Server IP: 64.71.164.2
Probable Submitter: 69.38.98.49
----
Manual Page -- http://www.php.net/manual/en/keyword.extends.php
Edit -- https://master.php.net/note/edit/72232
Del: integrated -- https://master.php.net/note/delete/72232/integrated
Del: useless -- https://master.php.net/note/delete/72232/useless
Del: bad code -- https://master.php.net/note/delete/72232/bad+code
Del: spam -- https://master.php.net/note/delete/72232/spam
Del: non-english -- https://master.php.net/note/delete/72232/non-english
Del: in docs -- https://master.php.net/note/delete/72232/in+docs
Del: other reasons-- https://master.php.net/note/delete/72232
Reject -- https://master.php.net/note/reject/72232
Search -- https://master.php.net/manage/user-notes.php