How to access object property with an illegal name with PHP?

Spread the love

Sometimes, we want to access object property with an illegal name with PHP.

In this article, we’ll look at how to access object property with an illegal name with PHP.

How to access object property with an illegal name with PHP?

To access object property with an illegal name with PHP, we can use curly braces.

For instance, we write

<?php
$x = new StdClass();
$x->{'todo-list'} = "eat";
echo $x->{'todo-list'};

to create object $x with StdClass.

Then we set the todo-list property of $x to 'eat'.

Then we get the value of the todo-list property with $x->{'todo-list'}.

Conclusion

To access object property with an illegal name with PHP, we can use curly braces.

Leave a Reply

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