title: Difference between call and execution pointcuts in PHP?
body: <p>In AOP in Java (AspectJ) when we talk about method pointcuts, we can differentiate them into two different sets: <code>method call pointcuts</code> and <code>method execution pointcuts</code>.</p>
  
  <p>Basing on these resources here on SO:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/18132822/execution-vs-call-join-point/18149106#18149106">execution Vs. call Join point</a></li>
  <li><a href="https://stackoverflow.com/questions/27376570/difference-between-call-and-execution-in-aop">Difference between call and execution in AOP</a></li>
  </ul>
  
  <p>And some AspectJ background, we can tell that basically the differences between the two can be expressed as the following:</p>
  
  <p>Given these classes:</p>
  
  <pre><code>class CallerObject {
        //...
        public void someMethod() {
           CompiletimeTypeObject target = new RuntimeTypeObject();
           target.someMethodOfTarget();
        }
        //...
  }
  
  class RuntimeTypeObject extends CompileTypeObject {
      @Override
      public void someMethodOfTarget() {
         super.someMethodOfTarget();
         //...some other stuff
      }
  }
  
  class CompiletimeTypeObject {
      public void someMethodOfTarget() {
         //...some stuff
      }
  }
  </code></pre>
  
  <ul>
  <li>A <strong>method call pointcut</strong> refers to the <em>call</em> of a method from a <em>caller</em> object which calls the method of a <em>target</em> object (the one which actually implements the method being called). In the example above, the caller is <strong>CallerObject</strong>, the target is <strong>RuntimeTypeObject</strong>. Also, a method call pointcut refers to the compile time type of the object, i.e. "CompiletimeTypeObject" in the example above;</li>
  </ul>
  
  <p>So a <code>method call pointcut</code> like this:</p>
  
  <pre><code>pointcut methodCallPointcut(): 
     call(void com.example.CompiletimeTypeObject.someMethodOfTarget())
  </code></pre>
  
  <p>Will match the <code>target.someMethodOfTarget();</code> join point inside the CallerObject.someMethod() method as the <code>compile type</code> of the RuntimeTypeObject is CompiletimeTypeObject, but this method call pointcut:</p>
  
  <pre><code>pointcut methodCallPointcut(): 
     call(void com.example.RuntimeTypeObject.someMethodOfTarget())
  </code></pre>
  
  <p>Will not match, as the compile time type of the object (CompiletimeTypeObject) is not a RuntimeTypeObject or a subtype of it (it is the opposite).</p>
  
  <ul>
  <li>A <strong>method execution pointcut</strong> refers to the <em>execution</em> of a method (i.e. <em>after</em> the method has been called or right <em>before</em> the method call returns). It doesn't give information about the caller and more important it refers to the <em>runtime type</em> of the object and not to the compile time type.</li>
  </ul>
  
  <p>So, both these method execution pointcuts will match the <code>target.someMethodOfTarget();</code> execution join point:</p>
  
  <pre><code>pointcut methodCallPointcut(): 
         execution(void com.example.CompiletimeTypeObject.someMethodOfTarget())
  
  pointcut methodCallPointcut(): 
         execution(void com.example.RuntimeTypeObject.someMethodOfTarget())
  </code></pre>
  
  <p>As the matching is based on the runtime type of the object which is RuntimeTypeObject for both and RuntimeTypeObject is both CompiletimeTypeObject (first pointcut) and a RuntimeTypeObject (second pointcut).</p>
  
  <p>Now, as PHP doesn't provide compile time types for objects (unless type-hinting is used to somehow emulate this behaviour), does it make sense to differentiate method call and method execution pointcuts in a PHP AOP implementation? How then will the pointcuts differ from each other?</p>
  
  <p>Thanks for the attention!</p>
  
  <p>EDIT: @kriegaex has pointed out another interesting aspect between call and method execution pointcuts in AspectJ. </p>
  
  <p>Thank you for the great and concise example. I have tried to make an example myself too and here is what I understood: </p>
  
  <p>In <em>case A (I use a 3rd party library)</em>, I actually can't intercept the execution of a library method because the library itself was already compiled into bytecode and any aspect concerning <em>that library</em> was <em>already woven</em> into that bytecode too (I would need to weave the sources in order to do so). </p>
  
  <p>So I can only intercept the method calls to the library methods, but again I can only intercept the <strong>calls</strong> to library methods <strong>in my code</strong> and <strong>not the calls to library methods from within the library itself</strong> because of the same principle (the calls to library methods from within the library itself are also already compiled). </p>
  
  <p>The same applies for System classes (same principle) as is said here (even if the reference refers to JBoss):</p>
  
  <p><a href="https://docs.jboss.org/jbossaop/docs/2.0.0.GA/docs/aspect-framework/reference/en/html/pointcuts.html" rel="nofollow noreferrer">https://docs.jboss.org/jbossaop/docs/2.0.0.GA/docs/aspect-framework/reference/en/html/pointcuts.html</a></p>
  
  <blockquote>
    <p>System classes cannot be used within execution expressions because it
    is impossible to instrument them.</p>
  </blockquote>
  
  <p>In case B (I provide a library for other users), if I actually need to intercept the usage of a method of my library either in the library itself or in the future user code which will use that method, then I need to use an execution pointcut as the aspect weaver will compile both the method execution and call pointcuts that <em>concern</em> my library <strong>and not the user code which will use my library methods</strong> (simply because the user code doesn't exist yet when I am writing the library), therefore using an execution pointcut will ensure that the weaving will occur <strong>inside</strong> the method execution (for a clear and intuitive example, look at the @kriegaex pseudo-code below) and not wherever the method is called within my library (i.e. at the caller side). </p>
  
  <p>So I can intercept the usage (more precisely, execution) of my library method both when the method is used within my library and in the user's code.
  If I had used a method call pointcut in this case, I would have intercepted only the calls made <em>from within</em> my library, and not the calls made in the user's code.</p>
  
  <p>Anyway, still think if these considerations make sense and can be applied in the PHP world, what do you think guys?</p>