Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redefine Built in PHP Functions

Tags:

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?

like image 574
Abs Avatar asked Feb 24 '10 14:02

Abs


People also ask

What are the PHP functions?

PHP Functions Parameterized Function PHP Call By Value PHP Call By Reference PHP Default Arguments PHP Variable Arguments PHP Recursive Function.

What is built-in and user-defined function in PHP?

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.

Which of the following is not a built-in function in PHP?

2. Which of the following is not a built-in function in php ? Explanation: fclosed() is not a built-in function in php.


2 Answers

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.

like image 57
Johannes Gorset Avatar answered Sep 19 '22 14:09

Johannes Gorset


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

like image 42
Gordon Avatar answered Sep 19 '22 14:09

Gordon