override_function

(PECL apd >= 0.2)

override_functionSurcharge les fonctions intégrées

Description

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

override_function() surcharge les fonctions intégrées (les remplace dans la table des symboles).

Liste de paramètres

function_name

La fonction à surcharger.

function_args

Les arguments de la fonction, séparés par une virgule.

Habituellement, vous voudriez passer ce paramètre, tout comme le paramètre function_code, délimité par un guillemet simple. La raison pour laquelle on utilise un guillemet simple est pour protéger le nom de la variable lors de l'analyse, sinon, si vous utilisez des guillemets doubles, il devient nécessaire d'échapper le nom de la variable, e.g. \$votre_variable.

function_code

Le nouveau code pour la fonction.

Valeurs de retour

Cette fonction retourne TRUE en cas de succès ou FALSE si une erreur survient.

Exemples

Exemple #1 Exemple avec override_function()

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

add a note add a note

User Contributed Notes 8 notes

up
94
php at undeen dot com
19 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
17
tothandor at gmail dot com
10 years 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
8
alextoemail at gmail dot com
9 years ago
Of course you can't overwrite "functions" like require_once or print as they are not really a function but a language construct.
up
7
Darien H
10 years 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
boban dot acimovic at gmail dot com
9 years ago
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');
up
0
fran
7 years ago
You can find  Excepción - Call to undefined function override_function()
due to Function override_function is part of PECL Extension.

Configure PECL
up
-1
pagan at o2 dot pl
16 years ago
There is not chance to override 2 or more functions, because of the error:
Fatal error: Cannot redeclare __overridden__()
up
-3
lange.ludo
9 years ago
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));
    
/* CODE that manipulates the result */
    
return $result;
}
?>

As a child :
<?php
function override_myFunction($arg,,$argN)
{   
/* CODE that manipulates the arguments */
    
return original_myFunction($arg,,$argN));
}
?>
To Top