Class not found

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • markusOk
    antwortet
    Kompliziert oder einfach

    Zitat von h3ll Beitrag anzeigen
    Es wäre einfacher, wenn der Code nicht so unnötig in mehrere Dateien und Ordner verstreut wäre. Ich blick bei der Ordnerstruktur ehrlich gesagt nicht durch. Und das ist für den Problemfall ja komplett irrelevant, also warum muss man sich damit abmühen?

    Es ist eigentlich deine Aufgabe ein simplifiziertes Beispiel zu erstellen, bei dem das Problem nachvollziehbar auftritt, als das andere sich in deine Anwendung hineindenken und verstehen müssen.
    Ok das verstehe ich, die Ordnerstruktur wollte ich jedoch bewusst als Teil der Problemstellung belassen, weil ich eine Lösung mit dieser forcieren wollte, da das Projekt erst am Anfang steht und noch stark skaliert werden sollte ohne das die Ordnung darunter leidet. -> https://ibb.co/WKRkj8g



    Danke für deinen Lösungscode, ich werde es mal so testen. Vielleicht kann ich es strukturell doch so machen wie von dir vorgeschlagen.

    Einen Kommentar schreiben:


  • h3ll
    antwortet
    Ich hab das ganze mal simplifiziert und so funktionierts:

    index.php:
    PHP-Code:
    <?php

    namespace {
        
    spl_autoload_register(function ($class) {
            
    // project-specific namespace prefix
            
    $prefix 'App\\';

            
    // base directory for the namespace prefix
            
    $base_dir __DIR__;

            
    // 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';

            
    // if the file exists, require it
            
    if (file_exists($file)) {
                require 
    $file;
            }
        });
    }

    namespace 
    App {
        
    $controller = new Post\PostController();
    }
    Post/PostController.php:
    PHP-Code:
    <?php

    namespace App\Post;

    class 
    PostController { }
    Die Backspaces bei den Namespaces macht das Forum kaputt, die muss man sich dazu denken.

    Einen Kommentar schreiben:


  • h3ll
    antwortet
    Es wäre einfacher, wenn der Code nicht so unnötig in mehrere Dateien und Ordner verstreut wäre. Ich blick bei der Ordnerstruktur ehrlich gesagt nicht durch. Und das ist für den Problemfall ja komplett irrelevant, also warum muss man sich damit abmühen?

    Es ist eigentlich deine Aufgabe ein simplifiziertes Beispiel zu erstellen, bei dem das Problem nachvollziehbar auftritt, als das andere sich in deine Anwendung hineindenken und verstehen müssen.

    Einen Kommentar schreiben:


  • markusOk
    antwortet
    Besserer Code?

    Ich denke, der Code ist eigentlich schon ziemlich generisch und sollte sofern die File-Struktur stimmt überall ausführbar sein. Hoffe das hilft:

    index.php
    PHP-Code:
    <?php
    namespace App;

    include_once(
    './startup/init.php');
    $controller = new PostPostController();
    ?>
    PostController.php
    PHP-Code:
    namespace AppPost;

    class 
    PostController{} 
    init.php
    PHP-Code:
    namespace App;

    require 
    __DIR__.'/autoload.php';
    require 
    __DIR__.'/dbconn.php'
    autoload.php
    PHP-Code:
    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());
        }
    }); 

    Einen Kommentar schreiben:


  • h3ll
    antwortet
    Bitte Code posten, den man auch selber bei sich ausführen kann.

    Einen Kommentar schreiben:


  • markusOk
    hat ein Thema erstellt Class not found.

    Class not found

    Fehler passiert auf Zeile 30 im index.php -> Class 'App\Post\PostController' not found in .../index.php. Kurioserweise beim Aufruf von get_declared_classes() in autoload.php gleich umittelbar nach implizitem Aufruf der Funktion wird App\Post\PostController als Klasse im Array aufgelistet

    Datenstruktur ist folgende:

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

    index.php
    PHP-Code:
    <?php
    namespace App;


    header('Access-Control-Allow-Origin: *');
    header('Content-Type: application/json; charset=UTF-8');
    header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
    header('Access-Control-Max-Age: 3600');
    header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');


    include_once(
    './startup/init.php');

    //var_dump(new Post\PostController());

    $uri parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    $uri_parts explode('/'$uri);

    $requestMethod $_SERVER['REQUEST_METHOD'];

    $ressourceClass $uri_parts[1];

    $id null;
    if(isset(
    $uri_parts[2])){
      
    $id = (int) $uri_parts[2];
    }

    switch (
    $ressourceClass) {
      case 
    'posts':
        
    $controller = new Post\PostController($dbconn$requestMethod$id);
        
    $controller->processRequest();
        break;

      default:
        
    header('HTTP/1.1 404 Not Found');
        exit();
        
    // break;
    }


     
    ?>
    PostController.php
    PHP-Code:
    namespace App\Post;

    class 
    PostController****{...} 
    init.php
    PHP-Code:
    namespace App;

    echo 
    'autoload will load';
    require 
    __DIR__.'/autoload.php';
    require 
    __DIR__.'/dbconn.php'
    autoload.php
    PHP-Code:
    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());
            
    $controller = new App\Post\PostController(null'GET'1);
        }
    }); 
Lädt...
X