Last active
August 29, 2015 14:20
-
-
Save putWorkDev/c249f1729d7ce4325c31 to your computer and use it in GitHub Desktop.
Using PHP traits (original post: https://www.harrytorry.co.uk/php/using-php-traits/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| To use Traits, you need to be using PHP >=5.4. | |
| More info.: http://stackoverflow.com/questions/9205083/php-traits-vs-interfaces | |
| This is an example of how a trait works, taken straight from the docs; | |
| <?php | |
| class Base { | |
| public function sayHello() { | |
| echo 'Hello '; | |
| } | |
| } | |
| trait SayWorld { | |
| public function sayHello() { | |
| parent::sayHello(); | |
| echo 'World!'; | |
| } | |
| } | |
| class MyHelloWorld extends Base { | |
| use SayWorld; | |
| } | |
| $o = new MyHelloWorld(); | |
| $o->sayHello(); | |
| ?> | |
| This will output `Hello World!`. Nice and simple, no jargon. | |
| Trait Properties | |
| It’s no more difficult that the example above, it really is simple stuff. | |
| trait PropertiesTrait { | |
| public $x = 1; | |
| } | |
| class PropertiesExample { | |
| use PropertiesTrait; | |
| } | |
| $example = new PropertiesExample; | |
| $example->x; | |
| Avoiding naming conflicts | |
| You can rename functions to avoid conflicting names like so. If you try to overload variable names, you will get a fatal error. | |
| trait A { | |
| function addOne($v) { | |
| return $v+1; | |
| } | |
| } | |
| class MyClass { | |
| use A { | |
| addOne as pleaseAddOne; | |
| } | |
| function calc($v) { | |
| $v++; | |
| return $this->pleaseAddOne($v); | |
| } | |
| } | |
| Traits made from multiple traits | |
| Sometimes, your app is going to become a large chunk of code with foos and bars everywhere, so you’re going to want to group multiple traits together to help break your application up. | |
| trait add { | |
| public function add($a, $b) { | |
| return $a + $b; | |
| } | |
| } | |
| trait subtract { | |
| public function subtract($a, $b) { | |
| return $a - $b; | |
| } | |
| } | |
| trait basicMath{ | |
| use add, subtract; | |
| } | |
| class app { | |
| use basicMath; | |
| } | |
| $myObject = new app(); | |
| echo $myObject->add(2, 3) . " and " . $myObject->subtract(20, 13); // 5 and 7 | |
| Now you have lots of small pieces of logic that can be used anywhere, whilst still being completely manageable. | |
| Order of precedence | |
| <?php | |
| trait HelloWorld { | |
| public function sayHello() { | |
| echo 'Hello World!'; | |
| } | |
| } | |
| class TheWorldIsNotEnough { | |
| use HelloWorld; | |
| public function sayHello() { | |
| echo 'Hello Universe!'; | |
| } | |
| } | |
| $o = new TheWorldIsNotEnough(); | |
| $o->sayHello(); //Hello Universe! | |
| So all I’m trying to say, this sort of stuff is really simple and good to use. Don’t be afraid of using it. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment