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?
PHP Functions Parameterized Function PHP Call By Value PHP Call By Reference PHP Default Arguments PHP Variable Arguments PHP Recursive Function.
PHP has a large number of built-in functions such as mathematical, string, date, array functions etc. It is also possible to define a function as per specific requirement. Such function is called user defined function. A function is a reusable block of statements that performs a specific task.
2. Which of the following is not a built-in function in php ? Explanation: fclosed() is not a built-in function in php.
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.
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.
Another option would be to use http://antecedent.github.io/patchwork
Patchwork is a PHP library that makes it possible to redefine user-defined functions and methods at runtime, loosely replicating the functionality
runkit_function_redefine
in pure PHP 5.3 code, which, among other things, enables you to replace static and private methods with test doubles.
The latter doesn't work for native functions though
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With