Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Ntraits;
- error_reporting(E_ALL);
- ini_set('display_errors', 'on');
- class NotConsumedException extends \Exception
- {
- }
- trait MultiCallable
- {
- public function __call($name, $arguments)
- {
- $r = new \ReflectionClass($this);
- foreach (class_uses(get_called_class()) as $trait) {
- $slash = strrpos($trait, '\\');
- $traitSimpleName = $slash === FALSE ? $trait : substr($trait, $slash + 1);
- $methodName = '_callNette' . $traitSimpleName;
- if ($r->hasMethod($methodName)) {
- $m = $r->getMethod($methodName);
- $m->setAccessible(TRUE);
- try {
- return $m->invoke($this, $name, $arguments);
- } catch (NotConsumedException $ex) {
- }
- }
- }
- if ($r->getParentClass() !== FALSE && $r->getParentClass()->hasMethod('__call')) {
- parent::__call($name, $arguments);
- }
- }
- }
- trait Foo
- {
- protected function _callNetteFoo()
- {
- echo "callNetteFoo\n";
- throw new NotConsumedException;
- }
- }
- trait Bar
- {
- protected function _callNetteBar()
- {
- echo "callNetteBar\n";
- throw new NotConsumedException;
- }
- }
- trait Baz
- {
- protected function _callNetteBaz()
- {
- return "callNetteBaz";
- }
- }
- class Z
- {
- protected function __call($name, $arguments)
- {
- echo "parent class";
- }
- }
- class A extends Z
- {
- use MultiCallable, Foo, Bar;
- }
- (new A)->foo();
- class B
- {
- use MultiCallable, Foo, Bar, Baz;
- }
- echo (new B)->foo();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement