Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From comment below question:</p> <blockquote> <p>because I would like to install call event handlers on classes, without the classes knowing about it, so I would know that the user is calling a method from a class before that method is actually called. </p> </blockquote> <p>Solution: use a Decorator.</p> <pre><code>class EventDecorator { protected $_instance; public function __construct($instance) { $this-&gt;_instance = $instance; } public function __get($prop) { printf('Getting %s in %s', $prop, get_class($this-&gt;_instance)); return $this-&gt;_instance-&gt;$prop; } public function __set($prop, $val) { printf('Setting %s with %s in %s', $prop, $val, get_class($this-&gt;_instance)); return $this-&gt;_instance-&gt;$prop = $val; } public function __call($method, $args) { printf('Calling %s with %s in %s', $method, var_export($args, TRUE), get_class($this-&gt;_instance)); return call_user_func_array(array($this-&gt;_instance, $method), $args); } } </code></pre> <p>Then you can wrap any class into it:</p> <pre><code>class Foo { public $prop; public function doSomething() {} } $foo = new EventDecorator(new Foo); $foo-&gt;prop = 'bar'; // Setting prop with bar in Foo $foo-&gt;prop; // Getting prop in Foo $foo-&gt;doSomething('something'); // Calling doSomething with array (0 =&gt; 'something',) in Foo </code></pre> <p>This can be fleshed out to provide pre and post hooks. You could also make the Decorator use the <a href="http://sourcemaking.com/design_patterns/observer" rel="nofollow noreferrer">Subject/Observer Pattern</a> and fire events to whatever other object registered to the decorator. The above approach is more maintainable and understandable than monkeypatching random methods with runkit.</p> <p>Additional notes:</p> <ul> <li>You might be interested in the <a href="http://components.symfony-project.org/event-dispatcher/" rel="nofollow noreferrer">Symfonys EventDispatcher component</a>. </li> <li>If you are after Aspect Oriented programming, read <a href="http://sebastian-bergmann.de/archives/573-Current-State-of-AOP-for-PHP.html" rel="nofollow noreferrer">http://sebastian-bergmann.de/archives/573-Current-State-of-AOP-for-PHP.html</a> - it's from 2006 but there is not much changed in that field since then. </li> <li>If you are after horizontal reuse, you will like <a href="http://wiki.php.net/rfc/traits" rel="nofollow noreferrer">PHP's Trait functionality</a> that is supposed to be in one of the next few versions.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload