Dynamic Properties Name In Php
There is an object in PHP named $item , and in this object I have properties of title , title_cn, title_tw And I would like to create an function that auto generate the propertie
Solution 1:
You should first concatenate $item->title
onto set_lang()
and place that in a variable.
You can then use that variable to call the right property on the obj.
example
$itemLangTitle = $item->title . set_lang();
echo$item->$itemLangTitle
You can use variables as properties dynamically but be careful with user input as always!
On a sidenote you also need to make sure the property exists otherwise you'll get errors. So make sure that if this doesn't yield anything it should fallback to english naming or something unobstructive ;)
Post a Comment for "Dynamic Properties Name In Php"