How to programmatically find public properties of a class inside one of its methods

I have a Foo class with public and protected properties. Foo should have a non-static getPublicVars() method that returns a list of all public Foo properties (this is just an example that I know from outside the Foo object calling get_object_vars() will do this, and I don't need my getPublicVars() method).

Note. . It should also return dynamically declared properties assigned at run time to the class instance (object) that are not defined in the class definition.

Here is an example:

 class Foo{ private $bar = '123'; protect $boo = '456'; public $beer = 'yum'; //will return an array or comma seperated list public function getPublicVars(){ // thar' be magic here... } } $foo = new Foo(); $foo->tricky = 'dynamically added var'; $result = $foo->getPublicVars(); var_dump($result); // array or comma list with 'tricky' and 'beer' 

What is the most concise way to get only the public properties of an object from within its own class method, in which both public and protected are visible?

I watched:

  • What is the best way to look at an instance (object) of a PHP class to see all the public properties and methods available to it?

But this does not seem to be my question, as it points to the use of get_object_vars() outside the object.

+11
reflection oop php
source share
3 answers

As you already understood, the PHP assembly get_object_vars depends on the area. You want only the properties of the public object.

So, this function for the public option is not a big step:

 function get_object_public_vars($object) { return get_object_vars($object); } 

Calling this get_object_public_vars will give you only public properties, because it goes beyond the current object.

If you need a finer-grained control, you can also use ReflectionObject :

 (new ReflectionObject($this))->getProperties(ReflectionProperty::IS_PUBLIC); 

Which has the advantage that you do not need to introduce another function in the global namespace.

+17
source share

Doesn't work with php version> = 7
Thus, I can no longer recommend a solution.
Use reflection instead

To get public properties inside a class

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

The "trick" is that get_object_vars is called from the scope of call_user_func , not the scope of the object

no need for reflection, autonomous functions, closures, etc.

+15
source share

According to this article (written by Vance Lucas), you can create a new call scope inside the definition of the Foo class using the "anonymous" function, and then you can call get_object_vars() from the inside. This will allow you to get only public properties inside your class, even if they were created dynamically later from the outside.

So for your example, this would be:

 <?php class Foo { private $bar = '123'; protected $boo = '456'; public $beer = 'yum'; // return an array of public properties public function getPublicVars(){ $publicVars = create_function('$obj', 'return get_object_vars($obj);'); return $publicVars($this); } } $foo = new Foo(); $foo->tricky = 'dynamically added var'; $result = $foo->getPublicVars(); print_r($result); 

and the output will be:

 Array
 (
     [beer] => yum
     [tricky] => dynamically added var
 )

In the article mentioned above there is a second example that shows another way to do the same using so-called โ€œclosuresโ€ (from php 5.3), but for some reason it does not work for me with php v5.4 therefore private and protected properties remain included in the resulting array.

-2
source share

All Articles