Why can var_dump ascertain values of private variables, yet it can't when trying to access a single the property

前端 未结 3 927
生来不讨喜
生来不讨喜 2021-01-06 16:37

I have an object that is being thrown into the session array, and I want to run a foreach on the items property.

I can\'t seem to access it. I see that it\'s priva

3条回答
  •  情书的邮戳
    2021-01-06 17:02

    I see that it's private, but I can't help but wonder why var_dump can show me what the property contains yet I can't read the data as it throws a fatal error?

    Please understand you should use getter and setter methods as explained in the chosen answer.

    But for completion on why and how it can be read (could help in debugging). Well, the value is there but private methods are surrounded by NULL bytes (ASCII Value 0).

    So if you really want to see that value with a var_dump();

    $key = "\0_items\0";
    var_dump($fun->$key);
    

提交回复
热议问题