为PHP提供include()'d文件父变量作用域 [英] Giving PHP include()'d files parent variable scope

查看:102
本文介绍了为PHP提供include()'d文件父变量作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个包含文件在父范围内用于被调用的文件?下面的例子被简化了,但是做了同样的工作。



从本质上讲,函数会包含一个文件,但希望包含文件的范围是

main.php:

包含它的函数的范围被调用。 <!c $ c><?php 
if(!function_exists('myPlugin'))
{
function myPlugin($ file)
{
if file_exists($ file)
{
require $ file;
return true;
}
return false;
}
}

$ myVar ='something bar foo';

$成功= myPlugin('included.php');

if($ success)
{
echo $ myResult;
}

included.php: p>

 <?php 
$ myResult = strlen($ myVar);

预先感谢,

Alexander。

编辑: / h2>

我们这种感谢Chacha102的贡献。

现在也可以在课堂上调用!



main.php

 <?php 
class front_controller扩展控制器
{
public function index_page()
{
$ myVar ='hello!';

//这是使它工作的位。
//我知道,将它封装在extract()中是丑陋的,
//以及不能改变的参数数量...
extract(load_file('included。 php',get_defined_vars(),$ this));

var_dump($ myResult);
}
public function get_something()
{
return'foo bar';



函数load_file($ _ file,$ vars = array(),& $ c = null)
{
if(! file_exists($ _ file))
{
return false;
}
if(is_array($ vars))
{
unset($ vars ['c'],$ vars ['_ file']);
提取($ vars);
}
需要$ _file;
return get_defined_vars();
}

included.php:

 <?php 
$ myResult = array(
$ myVar,
$ c-> get_something()
);

如果您想引用一个方法,它必须是公开的,但结果如预期的那样:

  array(2){
[0] =>
字符串(6)hello!
[1] =>
string(7)foo bar
}

现在,真的没有任何实际用途,我想知道如何做到这一点的唯一原因是因为我很固执。这个想法进入了我的脑海,并且不会让它击败我:D

< rant> >
感谢所有参与者。除了哄我的人。这是一个足够简单的问题,现在已经发现(复杂的)解决方案存在。

拧紧它是否符合PHP的做事方式。曾告诉客户哦,不,我们不应该那样做,这不是正确的做事方式!?不要以为。
< / rant>



再次感谢Chacha102:)

解决方案

 函数include_use_scope($ file,$ defined_variables)
{
提取($ defined_variables);
包含($ file);
}

include_use_scope(file.php,get_defined_vars());

get_defined_vars()它被调用的范围。 extract()接受一个数组并将它们定义为局部变量。

 提取物(阵列( 测试=> 中你好)); 
echo $ test; // hello

$ vars = get_defined_vars();
echo $ vars ['test']; // hello

因此,达到了预期的效果。你可能想从变量中去掉超全球变量和东西,因为覆盖它们可能是不好的。

查看评论剥离坏的。



为了得到相反的结果,你可以这样做:

 函数include_use_scope($ file,$ defined_variables)
{
extract($ defined_variables);
返回include($ file);
}

extract(include_use_scope(file.php,get_defined_vars()));

include.php

  //做东西
return get_defined_vars();

但总而言之,我不认为你会得到预期的效果,因为这不是PHP如何构建的。

Is there anyway to for an included file to be used in a parent scope to the one it was called in? The following example is simplified, but does the same job.

In essence, a file will be included by a function, but would like the scope of the included file to be the scope of where the function that included it was called from.

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;
  }

included.php:

<?php
  $myResult = strlen($myVar);

Thanks in advance,
Alexander.

EDIT: Solution

Well, sort of, thanks to the contributions by Chacha102.
Can be called from within a class now too!

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();
  }

included.php:

<?php
  $myResult = array(
    $myVar,
    $c->get_something()
  );

If you want to reference a method it has to be public, but the result is as expected:

array(2) {
  [0]=>
  string(6) "hello!"
  [1]=>
  string(7) "foo bar"
}

Now, this doesn't really have any practical use, and the only reason I wanted to know how to do this is because I am stubborn. The thought came into my head, and would not let it defeat me :D

<rant>
Thanks to all who contributed. Except the person who boo'd me. It was a simple enough question, and have now figured out that (complicated) solution exists.
Screw whether it "conforms to the PHP way of doing things". Ever told a client "Oh no, we shouldn't do that, it's not the correct way to do things!"? Thought not.
</rant>

Thanks again to Chacha102 :)

解决方案

function include_use_scope($file, $defined_variables)
{
    extract($defined_variables);
    include($file);
}

include_use_scope("file.php", get_defined_vars());

get_defined_vars() gets ALL variables defined in the scope it is called in. extract() takes an array and defines them as local variables.

extract(array("test"=>"hello"));
echo $test; // hello

$vars = get_defined_vars();
echo $vars['test']; //hello

Thus, the desired result is achieved. You might want to strip out the superglobals and stuff from the variables however, as overwriting them might be bad.

Check out this comment for stripping the bad ones out.

In order to get the reverse, you could do something like this:

function include_use_scope($file, $defined_variables)
{
    extract($defined_variables);
    return include($file);
}

extract(include_use_scope("file.php", get_defined_vars()));

include.php

// do stuff
return get_defined_vars();

But all in all, I don't think you are going to get the desired effect, as this was not how PHP was built.

这篇关于为PHP提供include()'d文件父变量作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆