©
本文檔使用 php中文網手册 發布
(PECL apd >= 0.2)
override_function — Overrides built-in functions
$function_name
, string $function_args
, string $function_code
)Overrides built-in functions by replacing them in the symbol table.
function_name
The function to override.
function_args
The function arguments, as a comma separated string.
Usually you will want to pass this parameter, as well as the
function_code
parameter, as a single quote
delimited string. The reason for using single quoted strings, is to
protect the variable names from parsing, otherwise, if you use double
quotes there will be a need to escape the variable names, e.g.
\ $your_var .
function_code
The new code for the function.
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Example #1 override_function() example
<?php
override_function ( 'test' , '$a,$b' , 'echo "DOING TEST"; return $a * $b;' );
?>
[#1] lange.ludo [2015-11-03 10:37:26]
Maybe it's better to use overwritten function inside the override to code something like this :
<?php
rename_function('myFunction','original_myFunction');
override_function('myFunction','$arg,??,$argN','return override_myFunction($arg,??,$argN);');
?>
You may then give somehow "inheritance" to override_myFunction ??
As a parent :
<?php
function override_myFunction($arg,??,$argN)
{ $result=original_myFunction($arg,??,$argN));
return $result;
}
?>
As a child :
<?php
function override_myFunction($arg,??,$argN)
{
return original_myFunction($arg,??,$argN));
}
?>
[#2] alextoemail at gmail dot com [2015-09-04 21:47:04]
Of course you can't overwrite "functions" like require_once or print as they are not really a function but a language construct.
[#3] boban dot acimovic at gmail dot com [2015-01-14 12:30:18]
Yes you can if you rename the overridden function. So you first rename original function, then override it and finally rename the overridden one, something like this:
rename_function('feof', 'real_feof');
override_function('feof', '$handle', 'return true;');
rename_function("__overridden__", 'dummy_feof');
[#4] tothandor at gmail dot com [2014-06-20 12:14:25]
Overriden function name becomes __overridden__(). That's why you can't override two function, and that's how you can use the original function in the override.
[#5] Darien H [2014-02-11 21:11:14]
Please note that this function (as of v1.0.1 in PHP 5.3) will <b>not</b> override some important built-in "functions". Specifically, those which are actually statements/keywords, such as:
require
include
require_once
include_once
echo
print
I was hoping to use it to trace the chains of require/include activity among files in a large legacy project, but it seems APD will not do what I need.
[#6] pagan at o2 dot pl [2008-10-24 00:34:05]
There is not chance to override 2 or more functions, because of the error:
Fatal error: Cannot redeclare __overridden__()
[#7] php at undeen dot com [2005-03-10 12:07:11]
I thought the example was not very helpful, because it doesn't even override the function with another function.
My question was: If I override a function, can I call the ORIGINAL function within the OVERRIDING function?
ie, can I do this:
<?php
override_function('strlen', '$string', 'return override_strlen($string);');
function override_strlen($string){
return strlen($string);
}
?>
The answer: NO, you will get a segfault.
HOWEVER, if you use rename_function to rename the original function to a third name, then call the third name in the OVERRIDING function, you will get the desired effect:
<?php
rename_function('strlen', 'new_strlen');
override_function('strlen', '$string', 'return override_strlen($string);');
function override_strlen($string){
return new_strlen($string);
}
?>
I plan to use this functionality to generate log reports every time a function is called, with the parameters, time, result, etc... So to wrap a function in logging, that was what I had to do.