3

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

Giorgi
  • 609
  • 2
  • 15
  • 29
  • So, when executing new Core\Bootstrap() you get the Index.php require warning? Correct? – dbf Sep 08 '12 at 16:28
  • yes it appends Core/ from namespace thats why it does not find it because Index.php is inside Project/Controllers/ and not inside Core/Project/Controllers – Giorgi Sep 08 '12 at 16:30
  • Do you use any "use" statements for your files e.g. use Core\Bootstrap; then $app = new Bootstrap(); – gunnx Sep 08 '12 at 16:39
  • i just added use statemnts to all of the mand error messages keep changing meaning i need to fill up all classes with use and see if that works. I think thats the problem – Giorgi Sep 08 '12 at 16:43

1 Answers1

2

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

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

rr-
  • 14,303
  • 6
  • 45
  • 67
Rasmus Styrk
  • 1,296
  • 2
  • 20
  • 36
  • did same thing. I added use statements to all of them and now header is showing. I am new to this and didnt know much about "use" statements – Giorgi Sep 08 '12 at 16:49
  • Use statements is bad practice because you bring everything out in global namespace and then risk name collisions. It is also harder for other programmers to debug your code because it can be hard to trace classes etc.. – Rasmus Styrk Sep 08 '12 at 16:57
  • everything works now except Views. In my Core\View.php i have $header = new Project\Views\index\header(); this shows the header and also gives error saying could not find Project\Views\index\header.php my Loader suppose to change \ to / – Giorgi Sep 08 '12 at 17:05
  • I do use "use" quite a lot, didnt think it was bad practice, I've seen a lot of frameworks using them. It's certainly not as nice as Java or .NET way! – gunnx Sep 08 '12 at 18:12