Using a constant inside a static array
-
18-04-2021 - |
Question
Im on php 5.2.x
I have a loader class that looks like so:
class Loader {
static $account_nameClasses = array(
'DB' => '/home/account_name/public_html/includes/php/DB.php',
'Skin' => '/home/account_name/public_html/includes/php/Skin.php',
'API' => '/home/account_name/public_html/api/apiClass.php',
'Search' => '/home/account_name/public_html/includes/php/Search.php',
'User' => '/home/account_name/public_html/includes/php/User.php'
);
static function loader($className) {
$filename = self::$account_nameClasses[$className];
if(file_exists($filename)){
require_once $filename;
}else{
return false;
}
}
}
spl_autoload_register(array(account_name_Loader, 'loader'));
As you can see, I repeat /home/account_name/public_html/includes/php/ over and over.
I want to replace that with a constant (or anything else) so that I can easily change it if I move the location of the app.
I tried this (Any may forms of this), but it just doesn't work
const PHP_CLASSES_ROOT = '/home/account_name/www/';
static $account_nameClasses = array(
'DB' => self::PHP_CLASSES_ROOT.'DB.php',
'Skin' => self::PHP_CLASSES_ROOT.'Skin.php',
'Search' => self::PHP_CLASSES_ROOT.'Search.php',
'User' => self::PHP_CLASSES_ROOT.'User.php'
);
Any ideas?
La solution
You can modify the loader function:
class Loader {
const PHP_CLASSES_ROOT = '/home/account_name/public_html/includes/php/';
static $account_nameClasses = array(
'DB' => 'DB.php',
'Skin' => 'Skin.php',
'API' => 'apiClass.php',
'Search' => 'Search.php',
'User' => 'User.php'
);
static function loader($className) {
$filename = Loader::PHP_CLASSES_ROOT.self::$account_nameClasses[$className];
if(file_exists($filename)){
require_once $filename;
}else{
return false;
}
}
}
spl_autoload_register(array(account_name_Loader, 'loader'));
Of if the class names are used elsewhere you can create a function to build paths:
class Loader {
const PHP_CLASSES_ROOT = '/home/account_name/public_html/includes/php/';
static $account_nameClasses = array(
'DB' => 'DB.php',
'Skin' => 'Skin.php',
'API' => 'apiClass.php',
'Search' => 'Search.php',
'User' => 'User.php'
);
static function resolveClassPath($className) {
return Loader::PHP_CLASSES_ROOT . $filename;
}
static function loader($className) {
$filename = self::resolveClassPath($className);
if(file_exists($filename)){
require_once $filename;
}else{
return false;
}
}
}
spl_autoload_register(array(account_name_Loader, 'loader'));
Autres conseils
You can change your loader
function to prepend this constant to $filename
static function loader($className) {
$filename = self::PHP_CLASSES_ROOT . self::$account_nameClasses[$className];
/* ... */
}
When you call a static property, it's already assigned but has no idea PHP_CLASSES_ROOT
exists because its a property (cannot be invoked) and not a method.
class Loader {
const PHP_CLASSES_ROOT = '/home/bildr/www/';
static public function account_nameClasses() {
return array(
'DB' => self::PHP_CLASSES_ROOT.'DB.php',
'Skin' => self::PHP_CLASSES_ROOT.'Skin.php',
'Search' => self::PHP_CLASSES_ROOT.'Search.php',
'User' => self::PHP_CLASSES_ROOT.'User.php'
);
}
//...
}
print_r(Loader::account_nameClasses());
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow