tags:

views:

126

answers:

2

I would like to redefine certain functions in PHP that are already built for example, echo() or time() - I don't need to define these functions globally, just within a single script for testing.

I think this can be done in Perl but in PHP - Is this possible?

Thanks all for any help.

+8  A: 

runkit_function_redefine — Replace a function definition with a new implementation

Note: By default, only userspace functions may be removed, renamed, or modified. In order to override internal functions, you must enable the runkit.internal_override setting in php.ini.

Johannes Gorset
Does this have a global effect on all PHP scripts, or is this specific to a particular script?
Abs
Depends on the scope of the php.ini file you're editing.
Dolph
Editing the php.ini file will affect all PHP scripts running on that particular installation of PHP. The function redifinition itself, however, is exclusive to the scripts that call `runkit_function_redefine()`.
Johannes Gorset
Ah I see, excellent! Thank you FRKT. :)
Abs
+2  A: 

You might also want to check out

override_function() — Overrides built-in functions

from the Advanced PHP debugger package.

Having to redefine native PHP functions or language statements should ring an alarm bell though. This should not be part of your production code in my opinion, unless you are writing a debugger or similar tool.

Gordon
@Gordon - thanks for the suggestion. I just want to do this for testing purposes and not production code - you are right it does ring alarm bells!
Abs