真理:该函数提取出的变量遵循 变量作用域 的原则,见下图:
四种情况:
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; //可以打印
时间: 2024-10-14 20:51:18