2017-10-18 4 views
3

Kann Projekt in Kohana nicht starten. Ich habe es von GitHub geklont, als meine Datenbank-Informationen in der Konfigurationsdatei gesetzt und Fehler erhalten: Klasse kann nicht neu deklariert werden.Kohana: Klasse kann nicht redeclare

Ich habe 2 Methoden Autoload-Funktionen.

public static function auto_load($class, $directory = 'classes') 
    { 
     // Transform the class name according to PSR-0 
     $class  = ltrim($class, '\\'); 
     $file  = ''; 
     $namespace = ''; 

     if ($last_namespace_position = strripos($class, '\\')) 
     { 
      $namespace = substr($class, 0, $last_namespace_position); 
      $class  = substr($class, $last_namespace_position + 1); 
      $file  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR; 
     } 

     $file .= str_replace('_', DIRECTORY_SEPARATOR, $class); 

     if ($path = Kohana::find_file($directory, $file)) 
     { 
      // Load the class file 
      require_once $path; 


      // Class has been found 
      return TRUE; 
     } 

     // Class is not in the filesystem 
     return FALSE; 
    } 

    /** 
    * Provides auto-loading support of classes that follow Kohana's old class 
    * naming conventions. 
    * 
    * This is included for compatibility purposes with older modules. 
    * 
    * @param string $class  Class name 
    * @param string $directory Directory to load from 
    * @return boolean 
    */ 
    public static function auto_load_lowercase($class, $directory = 'classes') 
    { 
     // Transform the class name into a path 

     $file = str_replace('_', DIRECTORY_SEPARATOR, strtolower($class)); 

     if ($path = Kohana::find_file($directory, $file)) 
     { 
      // Load the class file 
       require_once $path; 


      // Class has been found 
      return TRUE; 
     } 
     // Class is not in the filesystem 
     return FALSE; 
    } 

Ich habe versucht, class_exists() hinzufügen, bevor require(), aber es nicht & funktioniert Was ich tun, um ein Projekt zu starten sollte?

+0

Können Sie vollständige Codebeispiel zeigen mit 'class_exists()' Nutzung? –

Antwort

0

Möglicherweise erforderliche Klasse ist bereits in einer anderen Datei enthalten. Ich weiß nicht, wie genau Sie Klassenkollisionsproblem zu überprüfen, aber man sollte so etwas tun:

function include_quietly($file) { 
$conflicts = false; 
$txt = file_get_contents($file); 
preg_match_all("#class\s+(\w+)\s*{#muis", $txt, $matches, PREG_SET_ORDER); 

foreach ($matches as $m) { 
    if (class_exists($m[1])) { 
    $conflicts = true; 
    break; 
    } 
} 

if (!$conflicts) 
    include_once $file; 
else 
    echo "include conflicts in file {$file}\n"; 
} 

include_quietly($path);