Get PHP class property by string
Like this
<?php$prop = 'Name';echo $obj->$prop;
Or, if you have control over the class, implement the ArrayAccess interface and just do this
echo $obj['Name'];
If you want to access the property without creating an intermediate variable, use the {}
notation:
$something = $object->{'something'};
That also allows you to build the property name in a loop for example:
for ($i = 0; $i < 5; $i++) { $something = $object->{'something' . $i}; // ...}
What you're asking about is called Variable Variables. All you need to do is store your string in a variable and access it like so:
$Class = 'MyCustomClass';$Property = 'Name';$List = array('Name');$Object = new $Class();// All of these will echo the same propertyecho $Object->$Property; // Evaluates to $Object->Nameecho $Object->{$List[0]}; // Use if your variable is in an array