2

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?

gadelat
  • 1,390
  • 1
  • 17
  • 25
  • Not possible... you could make `time` accept a callback, call it and pass the value to it. You'd still have to defined that callback though. – Felix Kling Jul 10 '11 at 10:36
  • If you're dealing with dates and time zones, the best way to go about it is using PHP's DateTime and DateTimeZone classes. Check www.php.net/DateTime for more info. If it's not what you're looking for then I missed the point completely :) – N.B. Jul 10 '11 at 10:41
  • @Felix Kling: defining a callback is not necessary in PHP >= 5.3 - you are able to use anonymous functions. – Tadeck Jul 10 '11 at 10:47
  • 1
    @Tadeck: You mean `time(function(){...})`? It is still a callback, it does not matter *how* you create it. – Felix Kling Jul 10 '11 at 11:02

3 Answers3

5

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.

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • apd installed on a shared hosting? I doubt it. Also APD isn't meant to work in production – dynamic Jul 10 '11 at 10:47
  • To be fair, shared hosting or production servers weren't mentioned. I'm just showing that if you want to override a built-in function, it can be done. – Paul Dixon Jul 10 '11 at 10:48
  • even tho production server isn't mentioned I think it's pretty oblivious he needs it for production. Anyway your note in your answer is fine now – dynamic Jul 10 '11 at 10:51
  • Why isn't it for production? Are there any side effects or is it slow or what? – gadelat Jul 10 '11 at 10:55
  • I think I'd agree that it's pretty *oblivious* too :) Seriously though, it isn't that obvious, he might just want it for testing. I've written unit tests where I've wanted to simulate the passage of time, though I've used different techniques to achieve that (dependancy injection) – Paul Dixon Jul 10 '11 at 10:59
  • @gadelet - APD gets deep into the internals of PHP in ways that might affect your ability to use other extensions. The documentation specifically mentions Zend Optimizer, so you really shouldn't consider this for production use. As a technique to aid testing or debugging, it's perfectly fine. – Paul Dixon Jul 10 '11 at 11:03
  • @Gadelat: are you going to use it only for testing or in your production? @Paul: let's see who is right lol – dynamic Jul 10 '11 at 11:09
  • Doesn't matter i was just curious, but sometimes i would definitelly try this in our real project and see what it will do (againg because of being curious :D) We does not use any php extensions or frameworks. – gadelat Jul 10 '11 at 11:13
2

You cannot do that.

Consider using date_default_timezone_set() and setlocale() too

dynamic
  • 46,985
  • 55
  • 154
  • 231
1

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();
Starx
  • 77,474
  • 47
  • 185
  • 261
  • I wanted to know what can i do if i used general php function in thousands places in project and i need to modify it. Renaming all of them would make huge effort and can lead to many mistakes. Last time i needed this temporarily because server didn't automatically change time to winter time and server administrators weren't in touch. Finally i resloved it by changing time zone, but right now time is wrong in +3 mins and server administrators are again not available. – gadelat Jul 10 '11 at 11:07
  • I dont see how renaming a function which you will be using so abundantly would lead to mistakes. Place them on a file and include them as header in every page. Same goes with CLASS function as well. – Starx Jul 10 '11 at 11:44
  • 1
    you can't rename built-in php function – gadelat Jul 10 '11 at 11:53