PHP Autoloader add Namespace path

My MVC structure is following:

- web
  -- Classes
    --- Loader.php
  -- Core
    --- Controller.php
    --- Model.php
    --- View.php
    --- Bootstrap.php
    --- DB.php
  -- Project
    --- Controllers (folder)
    --- Models (folder)
    --- Views (folder)

Now i have namespaces specified to each. For example i have

    namespace Classes; //for Loader.php
    namespace Core; //For Controller.php, Model.php etc...
    namespace Project\Controllers; //For Controllers inside Controllers folder etc...

My autoloader looks like this:

public static function Autoloader2($className) {
    $className = explode('\\', $className);
    $class = array_pop($className);
    $namespace = implode(DIRECTORY_SEPARATOR, $className);
    $file = $namespace . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';

   require $file;
}

and My main index.php uses

    spl_autoload_register( array('Loader', 'Autoloader2') );
    $app = new Core\Bootstrap();

when i open localhost/web/ i get the following error:

  Warning: require(Core/Project/Controllers/Index.php): failed to open stream: N

It seems it prepends Core/ to the requested file. Index.php is inside Project/Controllers and not Core/Project/Controllers. If i try to remove

  //namespace Core; from Bootstrap.php i get the following error
  Fatal error: Class 'Core\Bootstrap' not found 

Please help

Answers


You should use $app = \Core\Bootstrap();.

The slash before the path is important, without it your namespace paths will be relative to each other.


Need Your Help

int32_t to int64_t (or to float)

c casting

I have a very simple algorithm written in C, which takes values from array of int16_t, multiplies those values by their positions and stores into array of int32_t. There is another variable sum of ...

Adding a custom component to SceneBuilder 2.0

javafx-8 scenebuilder

I have the need to have a selection listener and select method on a pane to be able to monitor and present a highlight when a node is clicked on.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.