变量名实例化的类不起作用[关闭]

$controller = 'DefaultController';
require('app/controllers/'.$controller.'.php');
$object = new $controller;

This is failing at the new $controller, but not at the require.. I really have no ideas here

Fatal error: Class 'DefaultController' not found in C:\wamp\www\controller\index.php on line 31

app/controllers/DefaultController.php

<?php
namespace App\Controllers;

/**
* DefaultController
*/
class DefaultController
{

    public function __construct()
    {
        # code...
    }

    public function index() {

    }
}

edit: I've added the namespace, still the same result

$controller = 'DefaultController';
require('app/controllers/'.$controller.'.php');
$controller = 'App\Controller\\'.$controller;
$object = new $controller;

Fatal error: Class 'App\Controller\DefaultController' not found in C:\wamp\www\controller\index.php on line 33

edit 2: ah yes.. it's Controllers not Controller

Your class name is App\Controllers\DefaultController, not DefaultController.

require 'app/controllers/DefaultController.php';
$controller = 'App\Controllers\DefaultController';
$object = new $controller;

How you square that away with an autoloader/require statement is up to you.

Because you're using the wrong namespace.

Try with

$controller = 'App\Controllers\DefaultController';

If you use a namespace, the namespace prefix MUST be prepended to the class name before creating an instance with new.

You class exists inside the namespace and its name carries the namespace name.