zan*_*win 9 php scope function include
是否有一个包含文件在父范围内用于它所调用的文件?以下示例已简化,但执行相同的工作.
本质上,函数将包含一个文件,但是希望包含文件的范围是调用包含它的函数的范围.
main.php:
<?php
if(!function_exists('myPlugin'))
{
function myPlugin($file)
{
if(file_exists($file)
{
require $file;
return true;
}
return false;
}
}
$myVar = 'something bar foo';
$success = myPlugin('included.php');
if($success)
{
echo $myResult;
}
Run Code Online (Sandbox Code Playgroud)
included.php:
<?php
$myResult = strlen($myVar);
Run Code Online (Sandbox Code Playgroud)
先谢谢,
亚历山大.
嗯,有点,谢谢Chacha102的贡献.
现在也可以从课堂内调用!
main.php
<?php
class front_controller extends controller
{
public function index_page()
{
$myVar = 'hello!';
// This is the bit that makes it work.
// I know, wrapping it in an extract() is ugly,
// and the amount of parameters that you can't change...
extract(load_file('included.php', get_defined_vars(), $this));
var_dump($myResult);
}
public function get_something()
{
return 'foo bar';
}
}
function load_file($_file, $vars = array(), &$c = null)
{
if(!file_exists($_file))
{
return false;
}
if(is_array($vars))
{
unset($vars['c'], $vars['_file']);
extract($vars);
}
require $_file;
return get_defined_vars();
}
Run Code Online (Sandbox Code Playgroud)
included.php:
<?php
$myResult = array(
$myVar,
$c->get_something()
);
Run Code Online (Sandbox Code Playgroud)
如果要引用一个方法,它必须是公共的,但结果是预期的:
array(2) {
[0]=>
string(6) "hello!"
[1]=>
string(7) "foo bar"
}
Run Code Online (Sandbox Code Playgroud)
现在,这没有任何实际用途,我想知道如何做到这一点的唯一原因是因为我很固执.这个想法进入了我的脑海,不会让它打败我:D
<rant>
感谢所有贡献者.除了嘘我的人.这是一个简单的问题,现在已经发现存在(复杂的)解决方案.
搞砸它是否"符合PHP的做事方式".曾告诉客户"哦不,我们不应该这样做,这不是正确的做事方式!"?没想到.
</rant>
再次感谢Chacha102 :)
function include_use_scope($file, $defined_variables)
{
extract($defined_variables);
include($file);
}
include_use_scope("file.php", get_defined_vars());
Run Code Online (Sandbox Code Playgroud)
get_defined_vars()获取在它被调用的范围内定义的所有变量。extract()获取一个数组并将它们定义为局部变量。
extract(array("test"=>"hello"));
echo $test; // hello
$vars = get_defined_vars();
echo $vars['test']; //hello
Run Code Online (Sandbox Code Playgroud)
因此,达到了预期的结果。但是,您可能希望从变量中去除超全局变量和内容,因为覆盖它们可能很糟糕。
查看此评论以去除不良内容。
为了得到相反的结果,您可以执行以下操作:
function include_use_scope($file, $defined_variables)
{
extract($defined_variables);
return include($file);
}
extract(include_use_scope("file.php", get_defined_vars()));
Run Code Online (Sandbox Code Playgroud)
包含.php
// do stuff
return get_defined_vars();
Run Code Online (Sandbox Code Playgroud)
但总而言之,我认为您不会获得预期的效果,因为这不是 PHP 的构建方式。