phpautoloadpsr-0

How can I initialize a class without an package, autoloading


require_once __DIR__.'/lib/Symfony/Component/ClassLoader/UniversalClassLoader.php';

use Symfony\Component\ClassLoader\UniversalClassLoader;

$loader = new UniversalClassLoader();
$loader->registerNamespace('Test', __DIR__ . '/lib');
$loader->register();

$router = new Test\Router;

But what If I don't want to add the namespace Test when I initialize my classes, is that possible?

Like: $router = new Router;

...

<?php
namespace Test;

class Router
{
    public function __construct() {
        echo __CLASS__;
    }
}

Solution

  • Add another use statement like you see at the top:

    use Test\Router;

    Now you can:

    $router = new Router();