TIL: stdObject custom string keys

Hi there!
For quite some time I’ve been using objects in a wrong way. Every time after a json_decode, I was by inertia casting the returned stdObject into an array to use with some more “special” keys. The ones with special symbols, numbers, dashes and such.

But after some research, a new way of doing this caught my eye. Apparently if you write down the key within a couple {} it can be typed as if it were a string. Looks something like this:

// use the same key
$obj = (object) array("1my-sample$-key" => "hi there!");

// Wrong way! Don't do it like that
print_r($obj->1my-sample$-key); // Parse error: syntax error, unexpected '1' (T_LNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'

// pull the value from the object
print_r($obj->{'1my-sample$-key'}); // hi there!

Thanks for reading!

If you have a suggestion, please feel free to let me know below or on any social media!

Dragomir Țurcanu
Dragomir Țurcanu

Dragomir is passionate about technology and blogging. Having been developing software professionally since 2017, he can share the tricks he found along the way and document the technologies he is currently learning.

Articles: 17

Leave a Reply

Your email address will not be published. Required fields are marked *