note 99900 added to keyword.extends
| From: | bilalghouri at live dot com | Date: | Tue, 14 Sep 2010 08:33:20 +0000 |
| Subject: | note 99900 added to keyword.extends | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172534@lists.php.net to get a copy of this message | ||
One thing I figured out after a long time about extending a parent class that, if the child class
does not have any construct function, it will use its parent's construct.
for example:
class Main
{
public $a;
public function __construct()
{
echo '::Parent Class initiated::';
$this -> a = 'we are in the parent class';
}
}
class Child extends Main
{
public function getA()
{
return $this -> a;
}
}
$main = new Main();
$main -> child = new Child;
echo $main -> child -> getA();
//Output - ::Parent Class initiated::::Parent Class initiated::we are in the parent class
However, If we have a constructor in the child class as well:
class Child extends Main
{
public function __construct()
{
}
public function getA()
{
return $this -> a;
}
}
Then :
$main = new Main();
$main -> child = new Child;
echo $main -> child -> getA();
// Output - ::Parent Class initiated::
Note that the parent variable 'a' is not inherited by the child class if the constructor
from the parent class isnt called.
This behaviour of extension made me waste a lot of my precious time, as I could not understand why
some of my child classes were inheriting parent variables and some were not.
Hope this helps someone..
----
Server IP: 69.147.83.197
Probable Submitter: 118.103.227.199
----
Manual Page -- http://www.php.net/manual/en/keyword.extends.php
Edit -- https://master.php.net/note/edit/99900
Del: integrated -- https://master.php.net/note/delete/99900/integrated
Del: useless -- https://master.php.net/note/delete/99900/useless
Del: bad code -- https://master.php.net/note/delete/99900/bad+code
Del: spam -- https://master.php.net/note/delete/99900/spam
Del: non-english -- https://master.php.net/note/delete/99900/non-english
Del: in docs -- https://master.php.net/note/delete/99900/in+docs
Del: other reasons-- https://master.php.net/note/delete/99900
Reject -- https://master.php.net/note/reject/99900
Search -- https://master.php.net/manage/user-notes.php