Skip to content

Instantly share code, notes, and snippets.

@NeoBlack
Created July 15, 2015 10:08
Show Gist options
  • Save NeoBlack/46a0bb490b06a9e491bf to your computer and use it in GitHub Desktop.
Save NeoBlack/46a0bb490b06a9e491bf to your computer and use it in GitHub Desktop.
Example for PHP Traits
<?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('foo@bar.com'); // will send a mail
$comment = new Comment;
echo $comment->share(''); // 'share this item'
$comment->mail('foo@bar.com'); // 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