Detect if an object property is private in PHP
You can use Reflection to examine the properties of the class. To get only public and protected properties, profile a suitable filter to the ReflectionClass::getProperties
method.
Here's a quicky example of your makeString
method using it.
public function makeString()
{
$string = "";
$reflection = new ReflectionObject($this);
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($properties as $property) {
$name = $property->getName();
$value = $property->getValue($this);
$string .= sprintf(" property '%s' = '%s' <br/>", $name, $value);
}
return $string;
}
Check this code from http://php.net/manual/reflectionclass.getproperties.php#93984
public function listProperties() {
$reflect = new ReflectionObject($this);
foreach ($reflect->getProperties(ReflectionProperty::IS_PUBLIC /* + ReflectionProperty::IS_PROTECTED*/) as $prop) {
print $prop->getName() . "\n";
}
}
A quicker solution that I found:
class Extras
{
public static function get_vars($obj)
{
return get_object_vars($obj);
}
}
and then call inside of your testClass:
$vars = Extras::get_vars($this);
additional reading in PHP.net