PHP 中相当于 Python 的 locals() 的是什么?

Chr*_*ing 2 php python scope

我知道$GLOBALSPHP 中的 大致相当于 Python 中的globals(),但是有相当于 的吗locals()

我的Python:

>>> g = 'global'
>>> def test():
...     l = 'local'
...     print repr(globals());
...     print repr(locals());
... 
>>> 
>>> test()
{'g': 'global', [...other stuff in the global scope...]}
{'l': 'local'}
Run Code Online (Sandbox Code Playgroud)

我的 PHP 端口:

<?php
$g = 'global';
function test(){ 
    $l = 'local';
    print_r($GLOBALS);
    //...please fill in the dots...:-)
}
test();
?>
Array
(
    [g] => global
    [...other stuff in the global scope...]
)
Run Code Online (Sandbox Code Playgroud)

geo*_*org 6

get_defined_vars这就是您正在寻找的。

function test(){
    $a = 'local';
    $b = 'another';
    print_r(get_defined_vars());
}

test();

#Array
#(
#    [a] => local
#    [b] => another
#)
Run Code Online (Sandbox Code Playgroud)