note 107965 added to language.oop5.traits
| From: | SafakOzpinar/safakozpinaratgmail | Date: | Sun, 18 Mar 2012 03:03:15 +0000 |
| Subject: | note 107965 added to language.oop5.traits | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186723@lists.php.net to get a copy of this message | ||
Unlike inheritance; if a trait has static properties, each class using that trait has independent
instances of those properties.
Example using parent class:
<?php
class TestClass {
public static $_bar;
}
class Foo1 extends TestClass { }
class Foo2 extends TestClass { }
Foo1::$_bar = 'Hello';
Foo2::$_bar = 'World';
echo Foo1::$_bar . ' ' . Foo2::$_bar; // Prints: World World
?>
Example using trait:
<?php
trait TestTrait {
public static $_bar;
}
class Foo1 {
use TestTrait;
}
class Foo2 {
use TestTrait;
}
Foo1::$_bar = 'Hello';
Foo2::$_bar = 'World';
echo Foo1::$_bar . ' ' . Foo2::$_bar; // Prints: Hello World
?>
----
Server IP: 69.147.83.197
Probable Submitter: 78.173.199.56
----
Manual Page -- http://www.php.net/manual/en/language.oop5.traits.php
Edit -- https://master.php.net/note/edit/107965
Del: integrated -- https://master.php.net/note/delete/107965/integrated
Del: useless -- https://master.php.net/note/delete/107965/useless
Del: bad code -- https://master.php.net/note/delete/107965/bad+code
Del: spam -- https://master.php.net/note/delete/107965/spam
Del: non-english -- https://master.php.net/note/delete/107965/non-english
Del: in docs -- https://master.php.net/note/delete/107965/in+docs
Del: other reasons-- https://master.php.net/note/delete/107965
Reject -- https://master.php.net/note/reject/107965
Search -- https://master.php.net/manage/user-notes.php