1

Can I basically do something like:

register_function_hook('myFunctionHook');

so then when any function is run:

functionA(); //The hook runs myFunctionHook();
anoterFunction(); //The hook runs myFunctionHook();
Class::functionA(); //The hook runs myFunctionHook();

Does such a thing exist?

-- Edit --

What I want to do is to get a breakdown of durations of each function. Ie. Performance Tuning. I want to get an idea of what takes all the time without installing xDebug on my Apache server, however I don't know if it is possible.

2
  • 1
    Not AFAIK. What are you trying to accomplish? There might be a better way. Commented Feb 18, 2011 at 18:14
  • 1
    Check out Drupal. It uses hooks extensively! Commented Feb 18, 2011 at 20:39

5 Answers 5

7

It's possible with register_tick_function(), also check this comment on the PHP manual:

$script_stats = array();
$time = microtime(true);

function track_stats(){
    global $script_stats,$time;
    $trace = debug_backtrace();
    $exe_time = (microtime(true) - $time) * 1000;
    $func_args = implode(", ",$trace[1]["args"]);
    $script_stats[] = array(
        "current_time" => microtime(true),
        "memory" => memory_get_usage(true),
        "file" => $trace[1]["file"].': '.$trace[1]["line"],
        "function" => $trace[1]["function"].'('.$func_args.')',
        "called_by" => $trace[2]["function"].' in '.$trace[2]["file"].': '.$trace[2]["line"],
        "ns" => $exe_time
        );
    $time = microtime(true);
    }

declare(ticks = 1);
register_tick_function("track_stats");

// the rest of your project code

// output $script_stats into a html table or something

This "hooks" to everything, not just functions but I think it fits your purpose.

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, for my case, it does not hook into system calls. like mysqli_init.
3

No, its not possible the way you like

But You can achieve something close with inheritance.

 class Vehicle {
       function __construct() {
              $this->hookFunction();
       }

       function hookFunction() {
              //
       }
 }

 class Car extends Vehicle {

 } 

 Class Toyota extends Car {

 }

 new Toyota(); // will you hook function
 // this exclude static call to member functions, or other inline functions.

Comments

1

What you looking for is called profiler. And PQP looks like one, which is standalone.

1 Comment

But with that it looks like I have to basically put profiler elements all over my code, which is not what I want. I was hoping to have some kind of register.
1

Instead of polluting the code, you should use a real Profiler, like that one provided by xdebug

Comments

-1

Not sure if the Topic Starter needs this anymore, but perhaps others can still benefit from this.

There is a PHP lib, written completely in PHP, that allows you to do exactly what you want.

Here's an article about how it works, including the source code: http://phpmyweb.net/2012/04/26/write-an-awesome-plugin-system-in-php/

It allows you to register a function from a class to be hooked. So it basically executes your code first, and then you determine wether you want to call the original function too after your code has been executed.

2 Comments

the link is no longer valid :(
Archive, Gives clue to use static function wrap() { // self::PROTOCOL = "file"; stream_wrapper_unregister(self::PROTOCOL); stream_wrapper_register(self::PROTOCOL, __CLASS__); } To implement file protocol yourself so I guess you can textually inject yourself into any php file read. Unfortunately full code link inside is also dead. web.archive.org/web/20120623044534/http://phpmyweb.net/2012/04/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.