0

I'm using spl_autoload in my project, but when I try the following code, it gives me this error:

Fatal error: Class 'Router\Route' not found in

//Router File

<?php
namespace Router;
class Router{
function foo(){
new Route();
}
?>

//Route File

<?php
namespace Router;
class Route{}
?>

Any help? I'm kinda new with namespaces.

Tomer Simis
  • 193
  • 2
  • 4

1 Answers1

0

You can use a class from another namespace, but you can't have two namespaces by the same name (that defeats the point).

// In one file.
namespace Router;
class Router{
  function foo(){
    use Route as r;
    new r\Route();
  }
}

// In another file.
namespace Route;
class Route{}
donutdan4114
  • 1,262
  • 1
  • 8
  • 16
  • Both classes are on the same namespace here. – Guillaume Poussel Sep 20 '12 at 03:59
  • So it doesn't exists a way of refleting these namespaces in my directory structure? I have Router and Route inside the "Router" directory, so my spl autoload would search for them in this namespace. Is there a way of doing this? – Tomer Simis Sep 20 '12 at 09:18