php - Autoloading namespaces -


i creating own mvc framework simple website , want use namespaces don't know how autoload them. here tried far:

i have init.php file has autoloading function:

    spl_autoload_register(function($class){     $file = __dir__ . '/../' . str_replace('\\', '/', $class) .'.php';     echo $file;     if (file_exists($file)) {         require_once $file;         echo 'yes';     }      use app\core\app app;      $app = new app; 

this code returns "yes" locates app file.. returns fatal error: d:\xampp\htdocs\mvc\app/../app/core/app.phpyes fatal error: class 'app\core\app' not found in d:\xampp\htdocs\mvc\app\init.php on line 28 line 28 $app = new app;

here how namespacing app file:

    <?php      namespace app\core\app;      class app      {     ..... 

the file structure following:

| app |--| core |--|--| app.php ... | init.php 

any ideas why?

instead of making own autoloader, use one, comes built-in composer. add following segment in composer.json file , it's done:

"autoload": {   "psr-4": {     "": "dir_where_namespaces_start...usually_src"   } } 

also, can't make , "mvc framework", because mvc architecture application code implements , no framework.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -