简体   繁体   English

PHP魔术方法具有意外行为

[英]PHP Magic Methods with unexpected behaviour

<?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

?>

I've made this small class and code to ilustrate exactly what is my problem. 我制作了这个小类和代码来确切说明我的问题。

As you can see, my class has one property called $props. 如您所见,我的课程有一个称为$ props的属性。 This property will be a array with all other properties. 此属性将是具有所有其他属性的数组。 My array $props will be populated with Magic Method __set, when a instance of this class has an atribution at a nonexistent property ( http://php.net/manual/en/language.oop5.magic.php ). 当此类的实例在不存在的属性( http://php.net/manual/en/language.oop5.magic.php )上具有属性时,我的数组$ props将使用Magic Method __set填充。

This part works properly. 这部分工作正常。

When I want to read a nonexistent property, PHP should call MyClass's __get method and return the value stored in $props property. 当我想读取一个不存在的属性时,PHP应该调用MyClass的__get方法并返回存储在$ props属性中的值。 It works fine in first echo (value is correctly returned). 它在第一次回显中工作正常(正确返回值)。 But, if I use this reading action when I'm sending its return to one empty() function, it always returns TRUE, as you can see in second echo's return (in other words, php seems can't read $props property properly) 但是,如果在将它的返回值发送到一个empty()函数时使用此读取动作,则它始终返回TRUE,正如您在第二回显的返回值中所看到的(换句话说,php似乎无法正确读取$ props属性) )

If I store property's value in a $temp variable and use this variable, my problem is solved, BUT I don't want to just solve this problem. 如果我将属性的值存储在$ temp变量中并使用此变量,那么我的问题就解决了,但是我不想仅仅解决这个问题。 I want to understand what is happening here. 我想了解这里发生了什么。

Is it a PHP bug on Magic Methods? 这是魔术方法上的PHP错误吗? Is it a empty funcion bug? 它是一个空的funcion bug吗? Is my code doing something meaningless? 我的代码执行的操作没有意义吗? (Most probable answer) (最有可能的答案)

Before you ask: I'm running my php server on a Windows machine and my php version is 5.3.8 询问之前:我正在Windows机器上运行php服务器,而php版本是5.3.8

Thanks in advance. 提前致谢。

Because empty check if a variable exists and then if it is empty. 因为为empty请检查变量是否存在,然后是否为空。 If it doesn't exists (in your case because you're not passing it a variable), then it returns true 如果它不存在(在您的情况下,因为没有将其传递给变量),则它返回true

Checkout the doc page: http://php.net/manual/en/function.empty.php 检出doc页面: http : //php.net/manual/en/function.empty.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM