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:
But this doesn't seem to address my question as it points to using get_object_vars()
from outside the object.