Reputation: 804
I have a POPO (Plain Old PHP Object):
namespace App\Objects;
class POPO {
private $foo;
private $bar;
// getters and setters //
}
Elsewhere, I have a (Details - what the class does is unimportant) class that needs to know the names of the properties of POPO. POPO is not passed into this class, nor does this class instantiate POPO or care about the values of its properties.
class POPODetails {
private $POPOclassName = "App\Object\POPO"; //determined programatically elsewhere.
public getProperties(): array {
return get_class_vars($this->POPOClassName); //this will only return public properties.
}
}
To use get_object_vars
I would need to pass in an instantiated object, which I don't need otherwise, and would still only get public properties. I could use ReflectionClass::getProperties()
, but would also need to pass in an instantiated object.
So, is there a way to get a list of class vars using only the Fully Qualified Class Name?
Upvotes: 3
Views: 2909
Reputation: 4216
You can still use ReflectionClass
, php.net tells us the following about the constructor argument:
Either a string containing the name of the class to reflect, or an object.
so
<?php
class SomeClass
{
private $member;
private $othermember;
}
$cls = new ReflectionClass( SomeClass::class );
print_r( $cls->getProperties() );
will print:
Array
(
[0] => ReflectionProperty Object
(
[name] => member
[class] => SomeClass
)
[1] => ReflectionProperty Object
(
[name] => othermember
[class] => SomeClass
)
)
Upvotes: 7