开发者

how to "inject" built-in php functions

开发者 https://www.devze.com 2023-03-19 08:25 出处:网络
How can i modify returning value of built-in php function without creating new function with another name and renaming all of used functions with previous name to new one?

How can i modify returning value of built-in php function without creating new function with another name and renaming all of used functions with previous name to new one? e.g.

function time() {
    return time()-1000;
}

Of course this won't pass, isn't there something like "function time() extends 开发者_JAVA技巧time() {}" or similar?


With the APD PECL extension you can rename and override built-in functions.

//we want to call the original, so we rename it
rename_function('time', '_time()');

//now replace the built-in time with our override
override_function('time', '', 'return my_time();');

//and here's the override
function my_time($){
        return _time()-1000;  
}

APD is intended for debugging purposes, so this isn't a technique you should really consider for production code.


You cannot do that.

Consider using date_default_timezone_set() and setlocale() too


Why would you want to override PHP's function anyway? If some function does not do what you exactly want, then create your own function. If it overrides, use a different name or create a class and put the function inside it.

What you are trying to achieve is a wrong solution for a problem!!!

Some examples

Instead of function name time() you can make cTime()(custom Time) Just like I create my own function for print_r() as printr() to print array in my way

or something like

class FUNCTIONS {
    public function time() {
        return time()-1000;
    }
}
//Now access using
FUNCTIONS::time();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号