开发者

get_object_vars returns NULL

This line:

var_dump($data['department']);

returns this:

object(Penny\HomeBundle\Entity\Department)[1420]
  protected 'id' => int 37
  protected 'name' => string 'Support' (length=7)
  protected 'email' => string 'denis_nizetic@hotmail.com' (length=25)

When I try to do

var_dump(get_object_vars($data['department']));

I get

array
  empty

I have no idea why does this happen if the object is there.

Edi开发者_运维问答t: I fixed my problem with using $obj->getValue() methods (getters).

But the question is still there: why won't get_object_vars() work?


From manual:

Gets the accessible non-static properties

when your is protected.

Try get_class_vars.


get_object_vars only get the public properties of an object. Because you have only protected properties, nothing is returned.

If you want to get the protected, try to extending the class and executing get_object_vars from a method in this class.


This should be due to the "protected" visibility of your attributes. Does it display them if you change the fields to "public"?


It's because the variables are protected. get_object_vars can only see the accessible variables.

See http://php.net/get_object_vars


get_object_vars return only public properties for gettting list of protected properties you can use Reflection Examples: http://php.net/manual/en/reflectionclass.getproperties.php


Indeed, the function only returns public properties. The most sensible option to resolve this problem is to have a public method returning get_object_vars in your class and calling it from instance. I think setting all properties as public a too drastic change. With this method you don't have set properties to public and your class design remains untouched.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜