oop - PHP: autoloading multiple classes with namespaces -


i'm trying build own framework internal usage. got structure this:

index.php boot /     booter.php application /      controllers /            indexcontroller.php core /     template.class.php     model.class.php     controller.class.php     cache /           memcached.php     /          something.php 

booter.php contains: (it's working files located in core directory):

class booter { private static $controller_path, $model_path, $class_path;  public static function setdirs($controller_path = 'application/controllers', $model_path = 'application/models', $classes_path = 'core') {     self::$controller_path = $controller_path;     self::$model_path      = $model_path;     self::$class_path      = $classes_path;      spl_autoload_register(array('booter', 'loadclass'));     if ( debug )          debugger::log('setting dirs...'); }  protected static function loadclass($classname)  {     $classname = strtolower($classname);      if ( file_exists(dir . '/' . self::$model_path . '/' . $classname . '.php') )      {         require(dir . '/' . self::$model_path . '/' . $classname . '.php');     }            else if ( file_exists(dir . '/' . self::$class_path . '/' . $classname . '.class.php') )      {         require(dir . '/' . self::$class_path . '/' . $classname . '.class.php');     }     else if ( file_exists(dir . '/' . self::$controller_path . '/' . $classname . '.php') )     {         require(dir . '/' . self::$controller_path . '/' . $classname . '.php');     }      if ( debug )         debugger::log('autoloading classname: '.$classname); } } 

my application/controllers/indexcontroller looks this:

<? class indexcontroller extends controller {      public function actionindex()      {           $a = new model; // works           $a = new controller; //it works      } }  ?> 

and here comes questions:

[question 1]

my code working this:

$a = new model; // class model gets included core/model.class.php 

how can implement including files classes namespaces? example:

$a = new cache\memcached; // include file /core/cache/memcached.php $a = new anotherns\smth; // include file /core/anotherns/smth.php  

and on. how can produce handling of namespace?

[question 2]

is practice use single autoload classes, controllers , models or should define 3 different spl_autoload_register 3 different methods , why?

question 1:

in autoloader, change \ (for namespace) directory_separator. should work:

protected static function loadclass($classname)  {     $classname = strtolower($classname);     $classname = str_replace('\\', directory_separator, $classname); ... } 

always use directory_separator, if software has potential used on other platforms.

question 2:

i use 1 , separate classes namespace. however, think comes down how want structure framework , how separate code. else may able better answer question.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -