Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 all the public properties of Foo (this is just an example, I know from outside the Foo object calling get_object_vars() will accomplish this and there is no need for my getPublicVars() method).

Note: This must also return dynamically declared properties assigned at runtime to the class instance (object) that aren't defined in the class's definition.

Here's the 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 the only the public properties of an object from inside a class's own methods where both public and protected are visible?

I've looked at:

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

But this doesn't seem to address my question as it points to using get_object_vars() from outside the object.

like image 377
Ray Avatar asked Oct 29 '12 14:10

Ray


People also ask

How can we access properties and methods of a class in PHP?

Once you have an object, you can use the -> notation to access methods and properties of the object: $object -> propertyname $object -> methodname ([ arg, ... ] ) Methods are functions, so they can take arguments and return a value: $clan = $rasmus->family('extended');

What are class properties in PHP?

Data members declared inside class are called properties. Property is sometimes referred to as attribute or field. In PHP, a property is qualified by one of the access specifier keywords, public, private or protected. Name of property could be any valid label in PHP.


2 Answers

As you already realized, PHP's build in get_object_vars is scope-sensitive. You want the public object properties only.

So from that function to the public variant is not a large step:

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

Calling this get_object_public_vars will give you only the public properties then because it is place out of scope of the current object.

If you need more fine-grained control, you can also make use of the ReflectionObject:

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

Which has the benefit that you don't need to introduce another function in the global namespace.

like image 129
hakre Avatar answered Sep 27 '22 20:09

hakre


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

like image 23
Brad Kent Avatar answered Sep 27 '22 21:09

Brad Kent