Class not found in verschachtelter Struktur

markusOk

Grünschnabel
Hallo zusammen

Der Fehler passiert im index.php bei new Post\PostController() -> Class 'App\Post\PostController' not found in .../index.php. Beim Aufruf von get_declared_classes() in autoload.php nachdem die Klasse hinzugefügt wurde, wird App\Post\PostController als Klasse im Array aufgelistet. Trotzdem erhält man den oben genannten Fehler mit derselben Klassensignatur.

Datenstruktur ist folgende:

index.php
src/Post/ -> PostController.php
/startup/ -> autoload.php, dbconn.php, init.php

index.php
PHP:
<?php
namespace App;

include_once('./startup/init.php');
$controller = new PostPostController();
?>

PostController.php
PHP:
namespace AppPost;

class PostController{}

init.php
PHP:
namespace App;



require __DIR__.'/autoload.php';

require __DIR__.'/dbconn.php';

autoload.php
PHP:
spl_autoload_register(function ($class) {



    // project-specific namespace prefix

    $prefix = 'App\\';



    // base directory for the namespace prefix

    $base_dir = dirname(dirname(__FILE__)) . '/src/';



    // does the class use the namespace prefix?

    $len = strlen($prefix);

    if (strncmp($prefix, $class, $len) !== 0) {

        // no, move to the next registered autoloader

        return;

    }



    // get the relative class name

    $relative_class = substr($class, $len);

    // replace the namespace prefix with the base directory, replace namespace

    // separators with directory separators in the relative class name, append

// with .php

    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

    print 'File: '.$file;



    // if the file exists, require it

    if (file_exists($file)) {

        require $file;

        print_r(get_declared_classes());

    }

});
 
Zurück