I am trying to initialize some values inside a class and save them in constant and access them outside, in different 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?
A Class constant uses the const
keyword. You don't define them using the define function. Just like this:
class Config {
const TEST = "This is a constant";
}
// then use it:
var_dump(Config::TEST);
In PHP, you cannot dynamically set the value of a constant, but you can get a similar behaviour with a public static variable. ie.
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 downside is, there is nothing stopping other code from setting the value from 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());
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 the value then you dont want to use a constant you want to use a static property. IF you only want the Config
class to be able to manipulate the value of this property directly then use the private
or protected
keyword. If thats a non issue then you could use a public
property.
Another and perhaps most robust approach depending on what you are trying to implement is to use constants to access static or instance specific properties of the class:
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);
Not from your original code. But the following would work as a constant class variable.
class Config {
const TEST = "This is a Class Constant";
...
}
Accessed from anywhere that includes the Config
class declaration like:
echo Config::TEST;
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