简体   繁体   中英

How can I use one class inside another with the same namespace?

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.

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{}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM