note 107804 added to keyword.parent
| From: | greg-wilson at hotmail dot com | Date: | Tue, 06 Mar 2012 18:24:06 +0000 |
| Subject: | note 107804 added to keyword.parent | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186511@lists.php.net to get a copy of this message | ||
You can access/modify variables of the parent class from multiple subclasses:
class Superclass{
private static $arr = array();
protected addArrayElement($element){
self::$arr[] = $element;
}
protected printArray(){
var_dump( self::$arr );
}
}
class SubclassA extends Superclass{
public function addToArray($elem){
parent::addArrayElement($elem);
}
}
class SubclassB extends Superclass{
public function addToSameArray($elem){
parent::addArrayElement($elem);
}
}
$classA = new SubclassA;
$classA->addToArray("first");
$classB = new SubclassB;
$classB->addToSameArray("second");
calling '$classB->printArray()' results in both "first" and
"second" being printed, demonstrating shared static access to a superclass variable from
multiple extended classes.
----
Server IP: 69.147.83.197
Probable Submitter: 174.116.142.201
----
Manual Page -- http://www.php.net/manual/en/keyword.parent.php
Edit -- https://master.php.net/note/edit/107804
Del: integrated -- https://master.php.net/note/delete/107804/integrated
Del: useless -- https://master.php.net/note/delete/107804/useless
Del: bad code -- https://master.php.net/note/delete/107804/bad+code
Del: spam -- https://master.php.net/note/delete/107804/spam
Del: non-english -- https://master.php.net/note/delete/107804/non-english
Del: in docs -- https://master.php.net/note/delete/107804/in+docs
Del: other reasons-- https://master.php.net/note/delete/107804
Reject -- https://master.php.net/note/reject/107804
Search -- https://master.php.net/manage/user-notes.php