<?php
$m->type = 'EVENT';
if (empty($m->type)) {
var_dump($m->type);
}
?>
Run Code Online (Sandbox Code Playgroud)
这段代码打印出来
string(5) "EVENT"
Run Code Online (Sandbox Code Playgroud)
这怎么可能?
编辑
$ m对象是一个普通对象,使用magic __set和__get将值存储到受保护的数组中.
<?php
$m->type = 'EVENT';
if ($m->type == NULL) {
var_dump($m->type);
}
?>
Run Code Online (Sandbox Code Playgroud)
上面提到的代码按预期工作(它跳过if主体).
如果您在班级中使用魔法吸气剂,则文档页面会记录一个相当棘手的行为:
<?php
class Registry
{
protected $_items = array();
public function __set($key, $value)
{
$this->_items[$key] = $value;
}
public function __get($key)
{
if (isset($this->_items[$key])) {
return $this->_items[$key];
} else {
return null;
}
}
}
$registry = new Registry();
$registry->empty = '';
$registry->notEmpty = 'not empty';
var_dump(empty($registry->notExisting)); // true, so far so good
var_dump(empty($registry->empty)); // true, so far so good
var_dump(empty($registry->notEmpty)); // true, .. say what?
$tmp = $registry->notEmpty;
var_dump(empty($tmp)); // false as expected
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1328 次 |
| 最近记录: |