Es ist 1:00 Uhr, die Frist für die Lieferung Ihrer Webanwendung beträgt 8 Stunden ... und es funktioniert nicht. Wenn Sie versuchen herauszufinden, was los ist, füllen Sie Ihren Code mit var_dump () und sterben () überall, um zu sehen, wo der Fehler ist…
Sie sind verärgert. Jedes Mal, wenn Sie einen Rückgabewert oder eine variable Zuordnung ausprobieren möchten, müssen Sie Ihren Quellcode ändern, Ihre Anwendung ausführen und die Ergebnisse sehen. Am Ende sind Sie sich nicht sicher, ob Sie alle diese var_dumps entfernt haben oder nicht der Code. Ist Ihnen diese Situation bekannt?
psysh ist eine Read-Eval-Print-Schleife (oder Reply). Möglicherweise haben Sie zuvor eine Reply über die JavaScript -Konsole Ihres Browsers verwendet. Wenn Sie haben, wissen Sie, dass es viel Macht besitzt und nützlich sein kann, während Sie Ihren JS -Code debuggen.
Wenn Sie über PHP sprechen, haben Sie möglicherweise zuvor die interaktive Konsole von PHP (PHP -A) verwendet. Dort können Sie einen Code schreiben und die Konsole wird ihn ausführen, sobald Sie die Eingabetaste drücken:
php -a Interactive shell php > $a = 'Hello world!'; php > echo $a; Hello world! php >
Leider ist die interaktive Hülle keine Repon, da sie das „P“ fehlt (Druck). Ich musste eine Echo -Erklärung ausführen, um den Inhalt von $ a zu sehen. In einer echten Reply hätten wir es unmittelbar nach der Zuweisung des Werts gesehen.
Sie können PSYSH entweder mit einem Komponisten -G -Forderungsanweis oder des Herunterladens der ausführbaren PSYSH -Datei:
installieren:Komponist
composer g require psy/psysh:~0.1 psysh
Direkter Download (Linux/Mac)
wget psysh.org/psysh chmod +x psysh ./psysh
Zusätzlich können Sie es pro Projekt mit Composer aufnehmen, wie Sie später in diesem Artikel sehen werden.
Lassen Sie uns jetzt ein wenig mit Psysh spielen.
./psysh Psy Shell v0.1.11 (PHP 5.5.8 — cli) by Justin Hileman >>>
Die Haupthilfe wird Ihr bester Freund sein. Das gibt Ihnen alle möglichen Befehle und ihre Erklärungen:
>>> help help Show a list of commands. Type `help [foo]` for information about [foo]. Aliases: ? ls List local, instance or class variables, methods and constants. Aliases: list, dir dump Dump an object or primitive. doc Read the documentation for an object, class, constant, method or property. Aliases: rtfm, man show Show the code for an object, class, constant, method or property. wtf Show the backtrace of the most recent exception. Aliases: last-exception, wtf? trace Show the current call stack. buffer Show (or clear) the contents of the code input buffer. Aliases: buf clear Clear the Psy Shell screen. history Show the Psy Shell history. exit End the current session and return to caller. Aliases: quit, q
>>> help ls Usage: ls [--vars] [-c|--constants] [-f|--functions] [-k|--classes] [-I|--interfaces] [-t|--traits] [-p|--properties] [-m|--methods] [-G|--grep="..."] [-i|--insensitive] [-v|--invert] [-g|--globals] [-n|--internal] [-u|--user] [-C|-- category="..."] [-a|--all] [-l|--long] [target] Aliases: list, dir Arguments: target A target class or object to list. Options: --vars Display variables. --constants (-c) Display defined constants. --functions (-f) Display defined functions. --classes (-k) Display declared classes. --interfaces (-I) Display declared interfaces. --traits (-t) Display declared traits. --properties (-p) Display class or object properties (public properties by default). --methods (-m) Display class or object methods (public methods by default). --grep (-G) Limit to items matching the given pattern (string or regex). --insensitive (-i) Case-insensitive search (requires --grep). --invert (-v) Inverted search (requires --grep). --globals (-g) Include global variables. --internal (-n) Limit to internal functions and classes. --user (-u) Limit to user-defined constants, functions and classes. --category (-C) Limit to constants in a specific category (e.g. "date"). --all (-a) Include private and protected methods and properties. --long (-l) List in long format: includes class names and method signatures. Help: List variables, constants, classes, interfaces, traits, functions, methods, and properties. Called without options, this will return a list of variables currently in scope. If a target object is provided, list properties, constants and methods of that target. If a class, interface or trait name is passed instead, list constants and methods on that class. e.g. >>> ls >>> ls $foo >>> ls -k --grep mongo -i >>> ls -al ReflectionClass >>> ls --constants --category date >>> ls -l --functions --grep /^array_.*/ >>>
im Grunde genommen kann eine Replikation tun:
<span>>>> $a = 'hello'; </span><span>=> "hello" </span><span>>>></span>
Bitte beachten Sie, dass PsySH den $ a -Wert druckt, sobald er zugewiesen ist, wenn wir PsySH mit der interaktiven Konsole von PHP vergleichen.
Ein komplexeres Beispiel kann wie folgt sein:
php -a Interactive shell php > $a = 'Hello world!'; php > echo $a; Hello world! php >
Ich habe die Funktion Say () definiert und sie aufgerufen. Diese beiden Null, die Sie sehen, sind, weil weder die Funktionsdefinition noch ihre Ausführung einen Wert zurückgegeben haben (die Funktion spiegelt den Wert an). Während der Definition der Funktion änderte sich die Eingabeaufforderung von >>> zu ....
Können wir eine Klasse definieren und sie instanziieren?
composer g require psy/psysh:~0.1 psysh
Wenn ich Foo instanziierte, gab der Konstruktor einen Verweis auf das Objekt zurück. Aus diesem Grund druckte Psysh
wget psysh.org/psysh chmod +x psysh ./psysh
Wenn Sie zufällig vergessen haben, welche Methoden die Klasse Foo definiert hat, haben Sie jetzt die Antwort. Haben Sie eine Linux -OS- oder Mac -Befehlszeilenschnittstelle verwendet? Dann sind Sie mit dem LS -Befehl vertraut. Erinnere dich an die -la -Optionen?
./psysh Psy Shell v0.1.11 (PHP 5.5.8 — cli) by Justin Hileman >>>
süß, nicht wahr?
Die wahre Leistung vonpsysh leuchtet bei Integration in eine Webanwendung. Erstellen wir also eine.
Ich werde eine kurze Anwendung implementieren, um das Designmuster des Dekorationsdesigns zu präsentieren. Das UML -Klassendiagramm eines solchen Musters lautet wie folgt:
Machen Sie sich keine Sorgen, wenn Sie nicht viel über UML- oder Designmuster wissen. Für diesen Artikel ist es nicht erforderlich, sie zu verstehen.
Auch für dieses Projekt habe ich eine Reihe von Testfällen erstellt. Diese Testfälle können von Phpunit durchgeführt werden. Auch hier müssen Sie mit Unit -Tests nicht vertraut sein, um diesen Artikel zu verstehen.
Der vollständige Quellcode für diese kleine Anwendung finden Sie unter https://github.com/sitepoint-examples/psysh
Definieren wir unsere Datei Composer.json, um eine Abhängigkeit von PsySH zu erklären:
>>> help help Show a list of commands. Type `help [foo]` for information about [foo]. Aliases: ? ls List local, instance or class variables, methods and constants. Aliases: list, dir dump Dump an object or primitive. doc Read the documentation for an object, class, constant, method or property. Aliases: rtfm, man show Show the code for an object, class, constant, method or property. wtf Show the backtrace of the most recent exception. Aliases: last-exception, wtf? trace Show the current call stack. buffer Show (or clear) the contents of the code input buffer. Aliases: buf clear Clear the Psy Shell screen. history Show the Psy Shell history. exit End the current session and return to caller. Aliases: quit, q
Nach einer Komponist -Installation sollten Sie gut gehen.
Sehen Sie sich den folgenden Quellcode von der Datei public/decorator.php an. Es wird die SimpleWindow, DecoratedWindow und Titelwindow -Objekte instanziieren, um das Dekorateurmuster zu präsentieren:
>>> help ls Usage: ls [--vars] [-c|--constants] [-f|--functions] [-k|--classes] [-I|--interfaces] [-t|--traits] [-p|--properties] [-m|--methods] [-G|--grep="..."] [-i|--insensitive] [-v|--invert] [-g|--globals] [-n|--internal] [-u|--user] [-C|-- category="..."] [-a|--all] [-l|--long] [target] Aliases: list, dir Arguments: target A target class or object to list. Options: --vars Display variables. --constants (-c) Display defined constants. --functions (-f) Display defined functions. --classes (-k) Display declared classes. --interfaces (-I) Display declared interfaces. --traits (-t) Display declared traits. --properties (-p) Display class or object properties (public properties by default). --methods (-m) Display class or object methods (public methods by default). --grep (-G) Limit to items matching the given pattern (string or regex). --insensitive (-i) Case-insensitive search (requires --grep). --invert (-v) Inverted search (requires --grep). --globals (-g) Include global variables. --internal (-n) Limit to internal functions and classes. --user (-u) Limit to user-defined constants, functions and classes. --category (-C) Limit to constants in a specific category (e.g. "date"). --all (-a) Include private and protected methods and properties. --long (-l) List in long format: includes class names and method signatures. Help: List variables, constants, classes, interfaces, traits, functions, methods, and properties. Called without options, this will return a list of variables currently in scope. If a target object is provided, list properties, constants and methods of that target. If a class, interface or trait name is passed instead, list constants and methods on that class. e.g. >>> ls >>> ls $foo >>> ls -k --grep mongo -i >>> ls -al ReflectionClass >>> ls --constants --category date >>> ls -l --functions --grep /^array_.*/ >>>
Wir können den Code über PHP -CLI (Befehlszeilenschnittstelle) oder über einen Webserver ausführen, wenn einer konfiguriert ist. Wir können auch den internen Webserver von PHP verwenden.
Die Ausführung des obigen Codes über die Befehlszeilenschnittstelle sieht so aus:
<span>>>> $a = 'hello'; </span><span>=> "hello" </span><span>>>></span>
Wie können wir mit Psysh interagieren? Fügen Sie einfach psyshell :: debug (get_defined_vars ()) hinzu; Überall auf dem Code, in dem Sie Ihre Anwendung debuggen, in der Regel einfügen Sie eine Anweisung var_dump ():
>>> function say($a) { ... echo $a; ... } => null >>> say('hello'); hello => null >>>
Nach dem Speichern der Datei erhalten wir die folgende Ausgabe:
>>> class Foo ... { ... protected $a; ... ... public function setA($a) { ... $this->a = $a; ... } ... ... public function getA() { ... return $this->a; ... } ... } => null >>> $foo = new Foo(); => <Foo #000000001dce50dd000000002dda326e> {} >>> $foo->setA('hello'); => null >>> $foo->getA(); => "hello" >>>
Die Ausführung des Skripts wird suspendiert, und wir haben jetzt PsySHs Aufforderung zum Spielen. Ich bestehe Get_Defined_vars () als Parameter an psyshell :: debug (), also habe ich Zugriff auf alle definierten Variablen in der Shell:
>>> ls $foo Class Methods: getA, setA >>>
untersuchen wir die $ -Ffenstervariable:
php -a Interactive shell php > $a = 'Hello world!'; php > echo $a; Hello world! php >
Etwas Schönes, wenn es darum geht, Psysh in einer Anwendung zu haben, ist, dass wir den Quellcode eines instanziierten Objekts untersuchen können.
composer g require psy/psysh:~0.1 psysh
$ sofenster ist eine Instanz von SimpleWindow, die die Fensterschnittstelle implementiert. Ich frage mich, wie der Quellcode für die Fensterschnittstelle aussieht…
wget psysh.org/psysh chmod +x psysh ./psysh
Warum haben SimpleWindow und DecoratedWindow die gleiche Ausgabe? Untersuchen wir das $ decoratedWindow -Objekt.
./psysh Psy Shell v0.1.11 (PHP 5.5.8 — cli) by Justin Hileman >>>
Dieses Objekt ist "schwerer" als der SimpleWindow -
>>> help help Show a list of commands. Type `help [foo]` for information about [foo]. Aliases: ? ls List local, instance or class variables, methods and constants. Aliases: list, dir dump Dump an object or primitive. doc Read the documentation for an object, class, constant, method or property. Aliases: rtfm, man show Show the code for an object, class, constant, method or property. wtf Show the backtrace of the most recent exception. Aliases: last-exception, wtf? trace Show the current call stack. buffer Show (or clear) the contents of the code input buffer. Aliases: buf clear Clear the Psy Shell screen. history Show the Psy Shell history. exit End the current session and return to caller. Aliases: quit, q
>>> help ls Usage: ls [--vars] [-c|--constants] [-f|--functions] [-k|--classes] [-I|--interfaces] [-t|--traits] [-p|--properties] [-m|--methods] [-G|--grep="..."] [-i|--insensitive] [-v|--invert] [-g|--globals] [-n|--internal] [-u|--user] [-C|-- category="..."] [-a|--all] [-l|--long] [target] Aliases: list, dir Arguments: target A target class or object to list. Options: --vars Display variables. --constants (-c) Display defined constants. --functions (-f) Display defined functions. --classes (-k) Display declared classes. --interfaces (-I) Display declared interfaces. --traits (-t) Display declared traits. --properties (-p) Display class or object properties (public properties by default). --methods (-m) Display class or object methods (public methods by default). --grep (-G) Limit to items matching the given pattern (string or regex). --insensitive (-i) Case-insensitive search (requires --grep). --invert (-v) Inverted search (requires --grep). --globals (-g) Include global variables. --internal (-n) Limit to internal functions and classes. --user (-u) Limit to user-defined constants, functions and classes. --category (-C) Limit to constants in a specific category (e.g. "date"). --all (-a) Include private and protected methods and properties. --long (-l) List in long format: includes class names and method signatures. Help: List variables, constants, classes, interfaces, traits, functions, methods, and properties. Called without options, this will return a list of variables currently in scope. If a target object is provided, list properties, constants and methods of that target. If a class, interface or trait name is passed instead, list constants and methods on that class. e.g. >>> ls >>> ls $foo >>> ls -k --grep mongo -i >>> ls -al ReflectionClass >>> ls --constants --category date >>> ls -l --functions --grep /^array_.*/ >>>
Debuggen mit dem eingebetteten Server
<span>>>> $a = 'hello'; </span><span>=> "hello" </span><span>>>></span>
>>> function say($a) { ... echo $a; ... } => null >>> say('hello'); hello => null >>>
mit Psysh spielen
>>> class Foo ... { ... protected $a; ... ... public function setA($a) { ... $this->a = $a; ... } ... ... public function getA() { ... return $this->a; ... } ... } => null >>> $foo = new Foo(); => <Foo #000000001dce50dd000000002dda326e> {} >>> $foo->setA('hello'); => null >>> $foo->getA(); => "hello" >>>
>>> ls $foo Class Methods: getA, setA >>>
>>> ls -la $foo Class Properties: $a "hello" Class Methods: getA public function getA() setA public function setA($a)
" an
{ "name": "example/psysh", "authors": [ { "name": "John Doe", "email": "john@doe.tst" } ], "require": { "psy/psysh": "~0.1" }, "autoload": { "psr-4": {"Acme\": "src/"} } }
Wie können wir Psysh verwenden, um zu überprüfen, was los ist? Fügen Sie einfach die Shell :: Debug () -Methode hinzu, wie wir es zuvor getan haben.
<span><span><?php </span></span><span><span>chdir(dirname(__DIR__)); </span></span><span> </span><span><span>require_once('vendor/autoload.php'); </span></span><span> </span><span><span>use Acme<span>\Patterns\Decorator\SimpleWindow</span>; </span></span><span><span>use Acme<span>\Patterns\Decorator\DecoratedWindow</span>; </span></span><span><span>use Acme<span>\Patterns\Decorator\TitledWindow</span>; </span></span><span> </span><span><span>echo PHP_EOL . 'Simple Window' . PHP_EOL; </span></span><span> </span><span><span>$window = new SimpleWindow(); </span></span><span> </span><span><span>echo $window->render(); </span></span><span> </span><span><span>echo PHP_EOL . 'Decorated Simple Window' . PHP_EOL; </span></span><span> </span><span><span>$decoratedWindow = new DecoratedWindow($window); </span></span><span> </span><span><span>echo $decoratedWindow->render(); </span></span><span> </span><span><span>echo PHP_EOL . 'Titled Simple Window' . PHP_EOL; </span></span><span> </span><span><span>$titledWindow = new TitledWindow($window); </span></span><span> </span><span><span>echo $titledWindow->render();</span></span>
php public/decorator.php Simple Window +-------------+ | | | | | | | | | | +-------------+ Decorated Simple Window +-------------+ | | | | | | | | | | +-------------+ Titled Simple Window +-------------+ |Title | +-------------+ | | | | | | | | | | +-------------+
<span><span><?php </span></span><span><span>chdir(dirname(__DIR__)); </span></span><span> </span><span><span>require_once('vendor/autoload.php'); </span></span><span> </span><span><span>//... a lot of code here </span></span><span> </span><span><span>$titledWindow = new TitledWindow($window); </span></span><span> </span><span><span>echo $titledWindow->render(); </span></span><span> </span><span><span><span>\Psy\Shell</span>::debug(get_defined_vars()); //we want to debug our application here!</span></span>
php -a Interactive shell php > $a = 'Hello world!'; php > echo $a; Hello world! php >
Da ist der Fehler. Die Methode wiederholt den Wert, anstatt ihn zurückzugeben. Obwohl die Anwendung richtig zu funktionieren scheint, haben wir durch Unit -Tests und Psysh einen Defekt entdeckt und können ihn jetzt beheben.
Dieser Artikel sollte nicht erschöpfend sein, um alle potenziellen Psysh zu präsentieren. Es gibt andere coole Funktionen (wie "doc"), die Sie ausprobieren sollten. Psysh allein ist vielleicht nicht sehr nützlich, aber in Kombination mit anderen Tools und Ihren cleveren Debugging -Kräften kann es sich als wertvolles Kapital erweisen.
Wie kann ich die PSYSH -Konfiguration anpassen? Heimverzeichnis. In dieser Datei können Sie Konfigurationsoptionen wie Standardeinschluss, Fehlerebene und Befehlsverlaufsgröße festlegen. Sie können auch benutzerdefinierte Befehle oder Reinigungsmittel hinzufügen.
Kann ich Psysh mit meinem bevorzugten PHP -Framework verwenden? einschließlich Laravel, Symfony und Zend Framework. Einige Frameworks wie Laravel enthalten sogar Psysh für ihren Bastelbefehl.
Das obige ist der detaillierte Inhalt vonInteraktives PHP -Debuggen mit Psysh. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!