PHP : override_function doesn't override even if bool true is returned

社会主义新天地 提交于 2020-01-05 04:41:07

问题


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).



来源:https://stackoverflow.com/questions/12743159/php-override-function-doesnt-override-even-if-bool-true-is-returned

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!