
Originally Posted by
DrQuincy
Given point 3, are const properties also bad for unit testing/bad practice?
I just wanted to add a caveat to Jeff's answer to this. They're good and very useful providing their value is only ever used internally within the class. This is good:
PHP Code:
$ftp = new FTPConnection('server', 'username', 'password');
$ftp->setMode(FTPConnection::PASV);
Whereas this is bad:
PHP Code:
echo 'Total price: ' . Locale::CURRENCY . $total;
The difference is that due to encapsulation, the constant should only have meaning to the class which defined it. Using class constants outside the scope of the class which defined them is bad. The easiest way to see if you're breaking this is to answer the question: "If the value of the constant changes, will my code behave differently?" if you answer yes, then your code is breaking encapsulation and depending on the value of the constant. This is bad because the author of the class should be able to alter the value of the constant without breaking anyone else's code.
Bookmarks