Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could do this easily with the <a href="http://www.php.net/manual/en/language.oop5.overloading.php#object.call" rel="noreferrer"><code>__call</code></a> <a href="http://www.php.net/manual/en/language.oop5.magic.php" rel="noreferrer">magic method</a> and a generic "proxy" class which doesn't inherit directly from the base class.</p> <p>Here is a (near) complete implementation of a proxying class which wraps whatever object you pass it. It will invoke some "before" and "after" code around each method call.</p> <pre><code>class MyProxy { function __construct($object) { $this-&gt;object = $object; } function __call($method, $args) { // Run before code here // Invoke original method on our proxied object call_user_func_array(array($this-&gt;object, $method), $args); // Run after code here } } $base = new BaseClass(); $proxy = new MyProxy($base); $proxy-&gt;doSomething(); // invoke $base-&gt;doSomething(); </code></pre> <p>You would of course want to add a bit of error handling, like asking the proxied object if it responds to the given method in <code>__call</code> and raising an error if it doesn't. You could even design the Proxy class to be a base-class for other proxies. The child proxy classes could implement <code>before</code> and <code>after</code> methods.</p> <p>The downside is that your "child class" no longer implements <code>BaseClass</code>, meaning if you're using <a href="http://www.php.net/manual/en/language.oop5.typehinting.php" rel="noreferrer">type-hinting</a> and want to demand that only objects of type <code>BaseClass</code> are passed into a function, this approach will fail.</p>
    singulars
    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