In PHP is it possible to have constants with local scope? Is yes please provide a small example.
For more information on scope, read the manual section on variable scope . Note: As of PHP 7.1.0, class constant may declare a visibility of protected or private, making them only available in the hierarchical scope of the class in which it is defined. Using "define ('MY_VAR', 'default value')" INSIDE a class definition does not work as expected.
In PHP is it possible to have constants with local scope? Is yes please provide a small example. Yes, but only using a class. About Kalium's comment, if you're on PHP 5.3, you can indeed also use namespaces:
PHP constants follow the same PHP variable rules. For example, it can be started with a letter or underscore only. Conventionally, PHP constants should be defined in uppercase letters. Note: Unlike variables, constants are automatically global throughout the script. Use the define () function to create a constant. It defines constant at run time.
The default visibility of class constants is public . Class constants can be redefined by a child class. As of PHP 8.1.0, class constants cannot be redefined by a child class if it is defined as final . It's also possible for interfaces to have constants.
Yes, but only using a class.
class Foo
{
const BAR = 'hello, world';
}
print Foo::BAR;
About Kalium's comment, if you're on PHP 5.3, you can indeed also use namespaces:
namespace Foo;
const BAR = 1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With