Can I set a constant in a class and then access it in PHP?

I am trying to initialize some values ​​inside a class and store them in a constant and access them from the outside, in another part of my code.

<?php class Config { public static function initialize() { define('TEST',"This is a Constant"); } } $config = Config::initialize(); // do something with the constants 

Can I access it outside?

+4
php constants
Apr 19 2018-11-11T00:
source share
5 answers

The class constant uses the const keyword. You do not define them using the definition function. Similar:

 class Config { const TEST = "This is a constant"; } // then use it: var_dump(Config::TEST); 

In PHP, you cannot dynamically set a constant value, but you can get a similar behavior with a public static variable. i.e.

 class Config2 { public static $test = null; public static function initialize() { self::$test = "This is not a constant"; } } // Then use like Config2::initialize(); var_dump(Config2::$test); 

The disadvantage is that other code does not stop setting the value outside the class. If you need protection against this, you should use a getter function approach. eg.

 class Config3 { private static $_test = null; public static function initialize() { self::$_test = "This is not a constant, but can't be changed outside this class"; } public static function getTest() { return self::$_test; } } // Then use like Config3::initialize(); var_dump(Config3::getTest()); 
+7
Apr 19 '11 at 1:50
source share
 class Config { const TEST = "This is a Constant"; static public $test = "This is a static property test." static protected $test2; public static function initialize() { self::$test2 = 'initialized'; return self::$test2; } public static function getTest2() { return self::$test2; } } echo Config::TEST; // outputs This is a Constant echo Config::$test; // outputs This is a static property test. echo Config::initialize(); // outputs initialized; echo Config::getTest2(); // outputs initialized; 

If you need to dynamically set a value, you do not want to use the constant that you want to use the static property. IF you want the Config class to be able to directly manipulate the value of this property, use the private or protected keyword. If this is not a problem, you can use the public property.

Another and possibly the most reliable approach, depending on what you are trying to implement, is to use constants to access static or instance-specific class properties:

 class Config { const TEST = 0; const TEST2 = 1; protected static $conf = array(); public static function initialize($testVal, $test2Val) { $conf[self::TEST] = $testVal; $conf[self::TEST2] = $test2Val; } public static function get($key) { if(isset(self::$conf[$key]) { return self::$conf[$key]; } } } Config::initialize('Test', 'Test 2'); echo Config::get(Config::TEST); echo Config::get(Config::TEST2); 
+5
Apr 19 '11 at 1:45 a.m.
source share

Not from source code. But the following will work as a constant class variable.

 class Config { const TEST = "This is a Class Constant"; ... } 

Access from anywhere that includes a Config class declaration, for example:

 echo Config::TEST; 
+1
Apr 19 2018-11-11T00:
source share

Yes - you can access it as Config::TEST

0
Apr 19 2018-11-11T00:
source share

Perhaps all you need in this case is a private variable with getter methods or a class constant.

0
Apr 19 2018-11-11T00:
source share



All Articles