在静态数组中使用常量

时间:2012-01-22 22:56:24

标签: php static constants spl-autoload-register

我在php 5.2.x上

我有一个看起来像这样的加载程序类:

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'));

如您所见,我重复/ home / account_name / public_html / includes / php /一遍又一遍。

我想用常量(或其他任何东西)替换它,以便在我移动应用程序的位置时可以轻松更改它。

我试过这个(任何可能的形式),但它只是不起作用

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'
);

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

您可以修改加载程序功能:

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'));

如果在其他地方使用类名,您可以创建一个函数来构建路径:

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'));

答案 1 :(得分:0)

您可以更改loader功能,将此常量添加到$filename

static function loader($className) {
    $filename = self::PHP_CLASSES_ROOT . self::$account_nameClasses[$className];
    /* ... */
}

答案 2 :(得分:0)

当你调用一个静态属性时,它已被分配,但不知道PHP_CLASSES_ROOT是否存在,因为它的属性(不能被调用)而不是方法。

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());