Quantcast
Channel: PHPDeveloper.org
Viewing all articles
Browse latest Browse all 21

Davey Shafik: Class Constants, How Do They Work? (Or: You Learn Something New Every Day...)

$
0
0

Davey Shafik has posted a quick article to his site talking about class constants and something new he learned about them (and how it relates to the uniform variable syntax handling in PHP7).

Yesterday on Twitter there was a conversation started by Marco Pivetta regarding a particularly horrible bit of code he had spotted [that] creates a string using sprintf() by prefixing ::PARAMNAME with the result of calling get_class() on the $api variable, and then passes that string into constant() which will give you the value of a constant using it's string name.

The conversation continued with comments from Elizabeth Smith about why this workaround was needed in the past. Davey also suggests that it won't work as expected if the input is an object and not a string but a test from Trevor Suarez proved that incorrect as well (it does work). He ends the post talking about PHP7 and showing how, thanks to the uniform variable syntax changes, this same kind of handling can be done in many other ways too.

Link: http://daveyshafik.com/archives/69193-class-constants-how-do-they-work-or-you-learn-something-new-every-day.html

Viewing all articles
Browse latest Browse all 21

Latest Images

Trending Articles





Latest Images