首页 > Web开发 > 详细

【解迷糊】关于PHP的extract()函数提取出的变量的作用域问题

时间:2017-07-11 20:45:33      阅读:313      评论:0      收藏:0      [点我收藏+]

真理:该函数提取出的变量遵循 变量作用域 的原则,见下图:

技术分享

 

四种情况:

class Test
{
    public function dosome()
    {
        $arr = [‘x‘ => ‘xing‘, ‘y‘ => ‘ya‘];
        extract($arr);
    }

    public function get()
    {
        return $x;
    }
}

$test = new Test();
$test->dosome();
$a = $test->get();
echo $a;  //无法打印
$arr = [‘x‘ => ‘xing‘, ‘y‘ => ‘ya‘];
if (true) {
    extract($arr);
}
echo $x;  //可以打印
function test1()
{
    $arr = [‘x‘ => ‘xing‘, ‘y‘ => ‘ya‘];
    extract($arr);
}

test1();
echo $x;   //无法打印
$arr = [‘x‘ => ‘xing‘, ‘y‘ => ‘ya‘];
extract($arr);
echo $x;   //可以打印

 

【解迷糊】关于PHP的extract()函数提取出的变量的作用域问题

原文:http://www.cnblogs.com/xingyazhao/p/7152111.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!