gadelat
gadelat

Reputation: 1470

how to "inject" built-in php functions

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 time() {}" or similar?

Upvotes: 2

Views: 753

Answers (3)

Starx
Starx

Reputation: 79021

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();

Upvotes: 1

Paul Dixon
Paul Dixon

Reputation: 300975

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.

Upvotes: 5

dynamic
dynamic

Reputation: 48131

You cannot do that.

Consider using date_default_timezone_set() and setlocale() too

Upvotes: 2

Related Questions