note 117213 added to keyword.extends
| From: | AdelAbulaadel at magebinary dot com | Date: | Mon, 04 May 2015 01:56:50 +0000 |
| Subject: | note 117213 added to keyword.extends | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202420@lists.php.net to get a copy of this message | ||
You can always place the universal variables and functions into the deepest class. Then call or
rewrite it in child classes. See example.
<?php
class ClassA
{
public $var;
function __construct()
{
$this->setVar();
$this->getVar();
}
protected function getVar()
{
echo $this->var;
}
public function setVar()
{
$this->var = 1000;
}
}
class ClassAChildA extends ClassA
{
function __construct()
{
$this->setVar();
$this->getVar();
}
public function setVar()
{
$this->var = 1001;
}
}
class ClassAChildB extends ClassA
{
function __construct()
{
$this->var = 1002;
$this->getVar();
}
public function getVar()
{
echo $this->var;
}
}
//prints 1000
$parent = new ClassA();
//prints 1001
$childA = new ClassAChildA();
//prints 1002
$childB = new ClassAChildB();
?>
----
Server IP: 72.52.91.14
Probable Submitter: 203.96.195.180
----
Manual Page -- http://php.net/manual/en/keyword.extends.php
Edit -- https://master.php.net/note/edit/117213
Del: integrated -- https://master.php.net/note/delete/117213/integrated
Del: useless -- https://master.php.net/note/delete/117213/useless
Del: bad code -- https://master.php.net/note/delete/117213/bad+code
Del: spam -- https://master.php.net/note/delete/117213/spam
Del: non-english -- https://master.php.net/note/delete/117213/non-english
Del: in docs -- https://master.php.net/note/delete/117213/in+docs
Del: other reasons-- https://master.php.net/note/delete/117213
Reject -- https://master.php.net/note/reject/117213
Search -- https://master.php.net/manage/user-notes.php