note 120038 added to language.oop5.traits
| From: | marko at newvibrations dot net | Date: | Thu, 13 Oct 2016 12:51:49 +0000 |
| Subject: | note 120038 added to language.oop5.traits | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-206879@lists.php.net to get a copy of this message | ||
As already noted, static properties and methods in trait could be accessed directly using trait.
Since trait is language assisted c/p, you should be aware that static property from trait will be
initialized to the value trait property had in the time of class declaration.
Example:
<?php
trait Beer {
protected static $type = 'Light';
public static function printed(){
echo static::$type.PHP_EOL;
}
public static function setType($type){
static::$type = $type;
}
}
class Ale {
use Beer;
}
Beer::setType("Dark");
class Lager {
use Beer;
}
Beer::setType("Amber");
header("Content-type: text/plain");
Beer::printed(); // Prints: Amber
Ale::printed(); // Prints: Light
Lager::printed(); // Prints: Dark
?>
----
Server IP: 72.52.91.14
Probable Submitter: 213.149.51.135
----
Manual Page -- http://php.net/manual/en/language.oop5.traits.php
Edit -- https://master.php.net/note/edit/120038
Del: integrated -- https://master.php.net/note/delete/120038/integrated
Del: useless -- https://master.php.net/note/delete/120038/useless
Del: bad code -- https://master.php.net/note/delete/120038/bad+code
Del: spam -- https://master.php.net/note/delete/120038/spam
Del: non-english -- https://master.php.net/note/delete/120038/non-english
Del: in docs -- https://master.php.net/note/delete/120038/in+docs
Del: other reasons-- https://master.php.net/note/delete/120038
Reject -- https://master.php.net/note/reject/120038
Search -- https://master.php.net/manage/user-notes.php