Created
July 15, 2015 10:08
-
-
Save NeoBlack/46a0bb490b06a9e491bf to your computer and use it in GitHub Desktop.
Example for 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
<?php | |
trait SharableTrait { | |
public function share($item){ | |
return 'share this item'; | |
} | |
} | |
trait MailableTrait { | |
public function mail($to){ | |
mail($to, 'Foo', 'Body-Text'); | |
} | |
} | |
interface SharableInterface { | |
public function share($item); | |
} | |
interface MailableInterface { | |
public function mail($to); | |
} | |
class Post implements SharableInterface { | |
use SharableTrait; | |
use MailableTrait; | |
} | |
class Comment implements SharableInterface { | |
use SharableTrait; | |
} | |
$post = new Post; | |
echo $post->share(''); // 'share this item' | |
$post->mail('[email protected]'); // will send a mail | |
$comment = new Comment; | |
echo $comment->share(''); // 'share this item' | |
$comment->mail('[email protected]'); // will not work, Comment misses the trait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment