php - Using a constant inside a static array -



php - Using a constant inside a static array -

im on php 5.2.x

i have loader class looks 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{ homecoming false; } } } spl_autoload_register(array(account_name_loader, 'loader'));

as can see, repeat /home/account_name/public_html/includes/php/ on , over.

i want replace constant (or else) can alter if move location of app.

i tried (any may forms of this), 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?

you can modify 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{ homecoming false; } } } spl_autoload_register(array(account_name_loader, 'loader'));

of if class names used elsewhere can create function 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) { homecoming loader::php_classes_root . $filename; } static function loader($classname) { $filename = self::resolveclasspath($classname); if(file_exists($filename)){ require_once $filename; }else{ homecoming false; } } } spl_autoload_register(array(account_name_loader, 'loader'));

php static constants spl-autoload-register

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -