具有意外行为的PHP神奇方法
因为具有意外行为的PHP神奇方法,php,Php,因为为空检查变量是否存在,然后检查变量是否为空。如果它不存在(在您的情况下,因为您没有向它传递变量),那么它将返回true 签出文档页面:请记住,empty是一种语言构造,而不是函数,必须处理实际变量(除非您使用的是PHP>=5.5.0)。。。。现在请阅读有关魔法的方法。。。这是有道理的。谢谢谢谢我没有做最基本的事情:查看文档。 <?php class MyClass{ private $props; public function __constructor(){
为空
检查变量是否存在,然后检查变量是否为空。如果它不存在(在您的情况下,因为您没有向它传递变量),那么它将返回true
签出文档页面:请记住,
empty
是一种语言构造,而不是函数,必须处理实际变量(除非您使用的是PHP>=5.5.0)。。。。现在请阅读有关魔法的方法。。。这是有道理的。谢谢谢谢我没有做最基本的事情:查看文档。
<?php
class MyClass{
private $props;
public function __constructor(){
$this->props = array();
}
public function __get($field) {
return $this->props[$field];
}
public function __set($field, $value) {
$this->props[$field] = $value;
}
}
$myInstance = new MyClass();
$myInstance->a = "Some string";
echo "Property 'a' has value ",$myInstance->a,"<br />"; // First echo
echo "Is property 'a' empty? ",(empty($myInstance->a) ? "Yes, but it shouldn't!" : "No, as expected"); // Second echo
// Output:
// Property 'a' has value Some string
// Is property 'a' empty? Yes, but it shouldn't!
$temp = $myInstance->a;
echo 'Variable $temp has value ',$temp,"<br />";
echo 'Is variable $temp empty? ',(empty($temp) ? "Yes, but it shouldn't!" : "No, as expected");
// Output:
// Variable $temp has value Some string
// Is variable $temp empty? No, as expected
?>