/ / PHP-Abhängigkeit und Klassenpfadverwaltung - PHP, Abhängigkeiten, Klassenpfad

PHP-Abhängigkeit und Klassenpfadverwaltung - PHP, Abhängigkeiten, Klassenpfad

Eine Sache, die mich oft beunruhigt, ist, dass ich es nicht tuehaben ein Klassenpfad- und Abhängigkeitsverwaltungssystem in PHP. Gibt es einen Rahmen, den Sie vorschlagen können? Ich habe gehört, Pear ist ein gutes System, aber ich würde gerne wissen, was es sonst noch gibt.

Ein Beispiel wäre zu sagen ... Ich habe Dateien A.php, B.php und C.php, wobei A von B abhängt, wer von C. abhängt, wobei sich alle 3 in verschiedenen Ordnern befinden.

Also, sobald A.php B. enthält.PHP, es muss auch C.php enthalten. Die Eingabe von require_once ("C.php") in B.php würde nicht funktionieren, da require_once einen relativen Pfad zwischen A.php und C.php und nicht zwischen B.php und C.php aufrufen muss, was ärgerlich ist.

Antworten:

3 für die Antwort № 1

Für dieses Problem bevorzuge ich eher einen Autoloader. Es ist nicht schwer, ein robustes Skript zu erstellen, um bestimmte Dateien zu scannen und eine Liste von Klassen zu erstellen, die Dateien daraus zugeordnet sind. So mache ich das:

$classes = array();

//this is the main function, give it a file and it will map any
//classes it finds in the file to the path. How you find the files
//is up to you, only you know your directory structure, but I
//generally set up a few folders that hold my classes, and have
//the script recurse through those passing each file it finds through
//this function
function get_php_classes($file) {
global $classes;
$php_code = file_get_contents($file);
$tokens = token_get_all($php_code);
$count = count($tokens);

//uses phps own parsing to figure out classes
//this has the advantage of being able to find
//multiple classes contained in one file
for ($i = 2; $i < $count; $i++) {
if (   $tokens[$i - 2][0] == T_CLASS
&& $tokens[$i - 1][0] == T_WHITESPACE
&& $tokens[$i][0] == T_STRING) {

$class_name = $tokens[$i][1];
//now we map a class to a file ie "Autoloader" => "C:projectAutoloader.cls.php"
$classes[$class_name] = $file;
}
}
}

$fh = fopen("file_you_want_write_map_to", "w");
fwrite($fh, serialize($classes));
fclose($fh);

Dies ist das Skript, das die Dateizuordnungen generiert. Sie führen es einmal aus, wenn Sie eine neue Klasse hinzufügen. Hier ist der eigentliche Anwendungscode, der zum automatischen Laden verwendet werden kann:

class Autoloader {
private $class_map;

public function __construct() {

//you could also move this out of the class and pass it in as a param
$this->class_map = unserialize(file_get_contents($file_you_wrote_to_earlier));
spl_autoload_register(array($this, "load"));
}

private function load($className) {
//and now that we did all that work in the script, we
//we just look up the name in the map and get the file
//it is found in
include $this->class_map[$className];
}
}

Damit könnte noch viel mehr getan werden,nämlich Sicherheitsüberprüfung für verschiedene Dinge wie doppelte Klassen, die beim Erstellen der Autoload-Liste gefunden wurden, um sicherzustellen, dass Dateien vorhanden sind, bevor versucht wird, sie einzuschließen usw.


2 für die Antwort № 2

Ich empfehle Ihnen, das Doctrine Class Loader-Projekt auszuprobieren.

Hier Sie finden die offizielle Dokumentation.

Um diese Bibliothek nutzen zu können, benötigen Sie eine Version von PHP, die den Namespace unterstützt (dann> = 5.3).


1 für die Antwort № 3

Der Komponist ist der Ort, an dem alles läuft, und das alles für Sie ganz einfach

http://getcomposer.org/