How to programmatically find public properties of a class from inside one of it's methods

前端 未结 3 950
醉梦人生
醉梦人生 2021-02-12 20:54

I\'ve got a class Foo with public and protected properties. Foo needs to have a non-static method, getPublicVars() that returns a list of

3条回答
  •  别跟我提以往
    2021-02-12 21:10

    Does not work with php version >=7
    As such, I can't really recommend solution any longer.
    Use reflection instead

    To get the public properties from within the class

    $publicProperties = call_user_func('get_object_vars', $this);
    

    the "trick" is that get_object_vars is being called from the scope of call_user_func and not the scope of the object

    no need for reflection, stand-alone functions, closures, etc

提交回复
热议问题