我正在编程,遇到了这个问题: 在下面的代码示例中,公共函数设置了一个私有变量。现在人们会认为私有变量的内容是私有的,认为$ GLOBALS varriable(超全局)可以访问它,并且至少读取它。为什么?有没有一种方法可以做到这一点?为什么PHP私有类var不是私有的?
<?PHP
error_reporting(E_ALL);
class test {
private $test = '';
public function test()
{
$this->test = 'Can u see me?';
}
}
$b = new test();
$b->test();
pre($GLOBALS['b']);
// Result:
// test Object
// (
// [test:test:private] => Can u see me?
//)
somefunc();
function somefunc()
{
pre($GLOBALS['b']);
// Result:
// test Object
// (
// [test:test:private] => Can u see me?
//)
}
echo $b->test;
// Result:
// Fatal error: Cannot access private property test::$test
function pre($a) {
echo '<pre>';
print_r($a);
echo '</pre>';
}
?>
谢谢 杰弗里
您在测试'$ GLOBALS ['b'] - > test'是否会引发致命错误吗?因为它应该。 – 2012-02-06 22:18:43