TIL – Access object properties via variable

Have been struggling a lot since morning doing a simple stuff. Goal was to create a node object by adding the node field object properties dynamically. Everything looked fine but it was not working. After nearly burning around 4 hours I finally got the mistake and seriously makes me feel like a noob 😦 So lets say we have an object $obj which have a property test. So basically to put in data in the object we can access it like this and put in the value.

$obj->test = "Blah Blah!!"

But in my case I needed to insert the data accessing the property via a variable. I was trying like

$obj->$test = "Blah Blah!!"

And was getting error of illegal string offset. Tried many different ways to make it work but all was in vain. Finally got the solution. Need to wrap the variable to make it accessible by the object. So the proper working way will be:

$obj->{$test} = "Blah Blah!!"

Again, feeling like a noob in PHP programming after this. But  feels great because there is always something to learn everyday. #BeingOptimistic 🙂 Cheers.

Leave a comment