Get PHP Class Property by String

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'];

PHP: Access Object Properties by String

Finally I have working code.

With mental support from @NigelRen

Emotional support from @FunkFortyNiner

And most of the code from this question about arrays

Test Object:

$obj = json_decode('{"snow":{"elevation":"365.4","results":"6","status":1},"wind":{"elevation":"365.4","windi":"100 mph","windii":"110 mph","windiii":"115 mph","status":1}}');

Test Directory:

$path = 'wind:windii';

Getter:

  function get($path, $obj) {
$path = explode(':', $path);
$temp =& $obj;

foreach($path as $key) {
$temp =& $temp->{$key};
}
return $temp;
}
var_dump(get($path, $obj)); //dump to see the result

Setter:

  function set($path, &$obj, $value=null) {
$path = explode(':', $path);
$temp =& $obj;

foreach($path as $key) {
$temp =& $temp->{$key};
}

$temp = $value;
}
//Tested with:
set($path, $obj, '111');

Syntax for echoing a class property in a magic quote string

Yours is almost right:

echo "foo: {$obj->foo}";

The php reference for complex string syntax says:

Any scalar variable, array element or object property with a string
representation can be included via this syntax. Simply write the
expression the same way as it would appear outside the string, and
then wrap it in { and }. Since { can not be escaped, this syntax will
only be recognised when the $ immediately follows the {.


I should have also mentioned this at the time I answered, but the brackets aren't actually needed for this expression.

echo "foo: $obj->foo";

should work just fine.

Retrieving PHP property name as string

Use ReflectionClass::getProperties() to get a list of the object's properties.

$reflect = new ReflectionClass($foo);
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);

dynamic class property $$value in php

You only need to use one $ when referencing an object's member variable using a string variable.

echo $this->$string;

PHP - Get object properties by variable

This is how you can do it:

for ($i = 1; $i <= 20; $i++){
$propertyName = "bullet$i";
if ($objectName->$propertyName){
echo $info->$propertyName;
}
}

Though I think using an array instead of the object would be a better solution.

Accessing the property name of the first property in an stdClass in PHP

get_object_vars is useful to get the object properties as an array and work from there.



 $p1 = new StdClass();
$p1->PropertyAbc = "Product 1|Product 5";

$p2 = new StdClass();
$p2->PropertyXyz = "Product 2|Product 9|Product 10";

$p3 = new StdClass();
$p3->PropertyEfg = "Product 3|Product 12";

$products = [ $p1, $p2, $p3 ];
foreach ($products as $product) {
$productArray = get_object_vars($product);
$productPropName = array_keys($productArray)[0];
$productPropsValues = explode('|', array_values($productArray)[0]);
foreach ($productPropsValues as $productPropsValue) {
$result[] = $productPropName . '>' . $productPropsValue;
}
}

var_dump(implode('|', $result));

string(155) "PropertyAbc>Product 1|PropertyAbc>Product 5|PropertyXyz>Product 2|PropertyXyz>Product 9|PropertyXyz>Product 10|PropertyEfg>Product 3|PropertyEfg>Product 12"

How can I access an object property named as a variable in php?

Since the name of your property is the string '$t', you can access it like this:

echo $object->{'$t'};

Alternatively, you can put the name of the property in a variable and use it like this:

$property_name = '$t';
echo $object->$property_name;

You can see both of these in action on repl.it: https://repl.it/@jrunning/SpiritedTroubledWorkspace



Related Topics



Leave a reply



Submit