As I was working with c a long time ago, I remember const
is scope related definition, a named memory allocation - so it depends where it is defined like inside a function or in global scope etc, and although its value cannot be changed directly, it can be still changed using its memory address.
This cannot be implement in php.
What are constants in PHP is more or like what #define
does in C (except you cannot create a macro in PHP)
#define translation = "5z]&gqtyfr$";
is equivalent of php define('translation','5z]&gqtyfr$')
.
Class constants in php are scope related which works the same way as in most C-like languages that support OOP.
class MyClass {
const CONST_VALUE = 5;
}
echo MyClass::CONST_VALUE;
Here is some more on "static const" vs "#define" vs "enum"
With php define()
you can create constant with any kind of name (as long its not been defined before)
define(' not a good name for constant!', 3);
echo constant(' not a good name for constant!'); // echoes 3
While doing that you obviously cannot call that kind of constants without using constant()
function