Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hooking into function calls in php

A little background: At runtime I would like to be able to inspect the currently called functions javadoc-style documentation, to determine its formal (typed) declaration. This would allow runtime type checking (for simple and complex types) by means of reflection (at a cost) during debugging and testing, something that I would find immensely helpful.

So, in php I would like for a user defined function to get called whenever any other function is about to get called. That is, if a function foo() gets called, I would like to have my callHookHandler() function called immediately before.

One solution would be to implement __call() in all user defined classes, but that is both unwieldy and doesn't include support for functions defined outside classes, so I am looking for a better solution.

like image 801
Søren Boll Overgaard Avatar asked Jun 16 '11 09:06

Søren Boll Overgaard


2 Answers

This sounds a bit of a fun one so I'm going to give answering it a try.

I hope this helps you. Let me know how it goes.

So, what you are asking can be done, and here's how:

For Functions:

  1. Get all defined functions with $function = get_defined_functions().
  2. Loop through the $functions['user'] key and inspect each one with the ReflectionFunction class. You'll need to get:
    1. The comment using ->getDocComment()
    2. The arguments using ->getParameters()
  3. Do some magic (I'll let you figure out how to parse the comment using some regular extressions and match it up with the parameter list from the reflection. Don't forget optional parameters!)
  4. Rename the function using runkit_function_rename
  5. Generate code in a string that checks the parameters and calls the renamed function
  6. Generate a parameter list as a string
  7. Create a new function with runkit_function_add using the code you generated in step #5 and the parameter list from step #6.

For Classes:

  1. Get a list of classes with $classes = get_declared_classes();
  2. Loop through each one and inspect it with ReflectionObject and ->getMethods() to get the methods. Make sure that the class is not internal with ->isInternal() because we can't do anything about the internal classes.
  3. In an inner loop... go through each method using the ReflectionMethod class. Get the arguments and PHPDoc/JavaDoc comments just like you did with normal functions.
  4. Do the same thing you did with the functions only use runkit_method_add and runkit_method_rename instead.

Downsides:

You won't be able to do the checking on internal class methods and functions (which is fine because they won't have doc comments anyway).

This is a lot of work! I left a lot of parts up to your imagination to avoid this being the length of a short book.

Please send me this or open source it and let me know when you finish, I really want to use this myself. Contact info is on my website which is in my profile ;)

Alternatively:

You can use XDebug's function trace along with reflection then analyze the results after the fact so that you don't have to dynamically edit the code. If you want to write unit-test you could even automate it.

Hope type checking makes it into future versions of PHP and wait: https://wiki.php.net/rfc/typechecking

Notes:

This class reference has a potentially useful example of parsing docComments in the comments section of the page: http://us.php.net/manual/en/class.reflectionmethod.php

References

  • get_defined_functions
  • get_declared_classes
  • ReflectionFunction
  • ReflectionObject
  • ReflectionMethod
  • runkit
like image 105
Andrew Curioso Avatar answered Sep 25 '22 07:09

Andrew Curioso


Alternative way to hook into function call is to use a trick with namespaces: Intercepting Execution of System Functions in PHP

You can also use the Go! framework to define an aspect to intercept the execution of system functions automatically.

like image 38
lisachenko Avatar answered Sep 25 '22 07:09

lisachenko