5.5.15

override_function

(PECL apd >= 0.2)

override_functionOverride توابع داخل-ساخت

Description

bool override_function ( string $function_name , string $function_args , string $function_code )

Override توابع داخل ساخت با جایگزینی آنها در جدول نشانه‌ها.

Parameters

function_name

تابع برای override.

function_args

آرگومان‌های تابع به صورت رشته جدا شده توسط کاما.

شما عموما این پارامتر را همراه با پارامتر function_code به عنوان یک رشته جدا شده توسط ' می‌فرستید تا از نام متغیرها در برابر ترجمه محافظت کنید، در غیر این صورت اگر از " استفاده کنید از نام متغیرها باید عبور کنید به عنوان مثال \$your_var.

function_code

کد جدید برای تابع.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

Example #1 مثال override_function()

<?php
override_function
('test''$a,$b''echo "DOING TEST"; return $a * $b;');
?>

add a note add a note

User Contributed Notes 5 notes

up
11
php at undeen dot com
9 years ago
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.
up
2
Darien H
5 months ago
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.
up
1
tothandor at gmail dot com
1 month ago
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.
up
0
rojaro at gmail dot com
8 years ago
Since Apache 1 & 2 use diffrent methods (Unicode vs. UTF8) on Win32 platforms to encode urls, i've implemented the following workaround to get around this "bug" (which is actually known behaviour and wont get fixed). This workaround is really usefull when writing PHP scripts which have to work on all platforms (Windows, Linux, BSD etc.), must process URLs and must work under both Apache versions.

<?php
$httpd
= explode(' ', $_SERVER['SERVER_SOFTWARE']);
if(
substr($httpd[0], 0, 6)=='Apache' && substr($httpd[0], 7, 1)==2 && $httpd[1]=='(Win32)')
{
  if(isset(
$_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = str_replace('%2F', '/', rawurlencode(utf8_decode(rawurldecode($_SERVER['REQUEST_URI']))));
  if(isset(
$_SERVER['REDIRECT_URL'])) $_SERVER['REDIRECT_URL'] = str_replace('%2F', '/', rawurlencode(utf8_decode(rawurldecode($_SERVER['REDIRECT_URL']))));
 
override_function('urlencode', '$url', 'return str_replace("%2F", "/", rawurlencode(utf8_encode($url)));');
}
?>
up
-2
pagan at o2 dot pl
5 years ago
There is not chance to override 2 or more functions, because of the error:
Fatal error: Cannot redeclare __overridden__()
To Top