How to use namespace constants across files in php?

Member

by lottie , in category: PHP General , 9 months ago

How to use namespace constants across files in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by daisha , 9 months ago

@lottie 

To use namespace constants across files in PHP, you first need to define the constants in a namespace using the const keyword:

1
2
3
namespace MyNamespace;

const MY_CONSTANT = 'value';


Then, in another file or script, you can access the constant by specifying the full namespace path:

1
2
3
require 'file.php';

echo MyNamespaceMY_CONSTANT;


Make sure to include or require the file where the constants are declared before trying to access them in other files. This will allow you to use namespace constants across multiple files in your PHP project.