2

Let's say I am using Doctrine ORM in my project (Doctrine is just an example, I am looking for a solution that would work with any class)

I want to intercept a call to Doctrine\ORM\EntityRepository::createQueryBuilder

So, if somewhere in my code (or in any other library code) is issued a call to that method, I want to execute some code before the execution of the called method.

Any PHP version is fine for me, I don't need a minimal compatibility.

I took a look here but the solution requires that I have control on the instantiation of classes which is definitely not my case.

Any one has a clue how to achieve this?

Thanks

Community
  • 1
  • 1
ilyes kooli
  • 11,959
  • 14
  • 50
  • 79
  • No built-in "magic" way of doing this. You either do it at a lower level, via a PHP module (C), _or_ wrap your classes in "Decorators" like described in that question. – Sergiu Paraschiv Oct 29 '14 at 18:28
  • Well, they are not *my classes* otherwise I would not be here :) But I still have hope :) – ilyes kooli Oct 29 '14 at 18:31
  • can you change EntityRepository class? do you have access to that – wonde Oct 29 '14 at 18:35
  • I also need this info. :) – Apul Gupta Oct 29 '14 at 18:35
  • @wonde If I would be able to change EntityRepository then I would just edit the method and don't need to *intercept* any calls – ilyes kooli Oct 29 '14 at 18:38
  • You can extend `Doctrine\ORM\EntityRepository` and provide a custom implementation. Depending on who instantiates it and how it's instantiated you might need to extend some other classes too. – Sergiu Paraschiv Oct 29 '14 at 19:09
  • Extending is not an option, I have few dozens bundles, means few hundred classes using Doctrine, and all my changes will vanish within the next **composer update** – ilyes kooli Oct 29 '14 at 19:17

0 Answers0