I want to check if a local variable in a class is public or private. The reason is to create a function like this:
function ToArray() {
$arr = array();
forea
From the PHP documentation,
$foo = new Foo();
$reflect = new ReflectionClass($foo);
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($props as $prop) {
print $prop->getName() . "\n";
}
var_dump($props);
To get a list of all public attributes call get_object_vars()
.
(Hint: calling it by the object itself will return all attributes.)
Make sure to follow some naming convention with your private variables (like i prepend them with _
)
Then just return those variables that do not have _
in the beginning of their key.