PHP : override_function doesn't override even

2019-07-26 18:45发布

问题:

I'm using the ovverride_function installed from apd.so library via pecl

It doesn't seem to work as expected

This is my script

function my_require($path) {
    echo "HELLO\n";
    echo $path;
}


$b = override_function('require', '$path', 'return my_require($path);');
var_dump($b);
require './index.php';

What I expected was to see as output

bool(true)
HELLO
./index.php

Instead I got

bool(true)

Warning: require(./index.php): failed to open stream: No such file or directory in /var/www/test/script/test.php on line 14

So even if the function seemed to work (bool true) the require function still acts as the old one.

Any idea?

回答1:

require is not a function, it's a language construct like function or echo. Try calling require index.php (without the ()), it will still work while calling a normal function without the () wont. The documentation does not explicitly says it is, but it is listed under "Control Structures", so that is why override_function does not work for this require (or any other language constructs).