Bird
0
0

You want to create a class Config with constants for environment settings. Which approach correctly defines and accesses a constant ENV with value 'production' from outside the class?

hard📝 Application Q8 of 15
PHP - Classes and Objects
You want to create a class Config with constants for environment settings. Which approach correctly defines and accesses a constant ENV with value 'production' from outside the class?
Aclass Config { const ENV = 'production'; } echo Config::ENV;
Bclass Config { public $ENV = 'production'; } echo Config::ENV;
Cclass Config { const ENV = 'production'; } echo Config->ENV;
Dclass Config { define('ENV', 'production'); } echo Config::ENV;
Step-by-Step Solution
Solution:
  1. Step 1: Define constant correctly

    Use const keyword inside class to define ENV constant.
  2. Step 2: Access constant from outside class

    Use ClassName::CONSTANT syntax to access it.
  3. Final Answer:

    class Config { const ENV = 'production'; } echo Config::ENV; -> Option A
  4. Quick Check:

    Define with const and access with :: [OK]
Quick Trick: Define with const, access with ClassName::CONST [OK]
Common Mistakes:
  • Using public property instead of constant
  • Using -> instead of :: to access constant
  • Using define() inside class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes