码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-07-11 21:23:43      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:display   logs   extract   取出   技术分享   auto   test   rac   nbsp   

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

技术分享

 

四种情况:

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()函数提取出的变量的作用域问题

标签:display   logs   extract   取出   技术分享   auto   test   rac   nbsp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!