是上午1:00,您的Web應用程序的交付截止日期為8小時……而且行不通。 當您嘗試弄清楚發生了什麼時,您將代碼填充var_dump()和die()到處都在查看錯誤在哪裡…
>>你很生氣。每次您想嘗試返回值或變量分配時,都必須更改源代碼,執行應用程序並查看結果……最終,您不確定是否已將所有這些var_dump從代碼。這種情況很熟悉嗎?
>>直接下載(Linux/Mac)
php -a Interactive shell php > $a = 'Hello world!'; php > echo $a; Hello world! php >
現在讓我們玩一點psysh。
主要幫助將是您最好的朋友。這將為您提供各種命令及其解釋的原因:
>
composer g require psy/psysh:~0.1 psysh
基本上,一個替補可以做的是:
>wget psysh.org/psysh chmod +x psysh ./psysh
>請注意,如果我們將PSYS與PHP的交互式控制台進行比較,則PSYSH在分配後立即打印出$ A a值。
>一個更複雜的示例可以如下:
php -a Interactive shell php > $a = 'Hello world!'; php > echo $a; Hello world! php >
>
我們可以定義類並實例化嗎?
composer g require psy/psysh:~0.1 psysh
>
wget psysh.org/psysh chmod +x psysh ./psysh
./psysh Psy Shell v0.1.11 (PHP 5.5.8 — cli) by Justin Hileman >>>
與Web應用程序集成時,PSYS的真實力量會閃耀,所以讓我們構建一個。
演示應用>我將實施一個快速應用程序來展示裝飾器設計模式。這種模式的UML類圖如下:
>如果您對UML或設計模式不了解,則不必擔心本文不需要理解它們。
>
可以在https://github.com/sitepoint-examples/psysh上找到此小應用程序的完整源代碼
首先,讓我們定義我們的composer.json文件以聲明對psysh的依賴性:
作曲家安裝後,您應該很好。
>>>> 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
我們可以通過PHP的CLI(命令行接口)執行代碼,或者如果配置了網絡服務器。 我們也可以使用PHP的內部Web服務器。
>>>> 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_.*/ >>>
中調試 通過命令行界面執行上述代碼的執行方式將如下所示:
>>
<span>>>> $a = 'hello'; </span><span>=> "hello" </span><span>>>></span>
>腳本的執行將被暫停,我們現在有PSYS的提示可以進行。我將get_defined_vars()作為參數傳遞給psyshell :: debug(),所以我可以訪問shell中的所有定義變量:
>>> function say($a) { ... echo $a; ... } => null >>> say('hello'); hello => null >>>
讓我們檢查$ window變量:
php -a Interactive shell php > $a = 'Hello world!'; php > echo $a; Hello world! php >
在應用程序中將psys添加到一個很好的是,我們可以檢查實例化對象的源代碼。
composer g require psy/psysh:~0.1 psysh
>
wget psysh.org/psysh chmod +x psysh ./psysh
>
./psysh Psy Shell v0.1.11 (PHP 5.5.8 — cli) by Justin Hileman >>>
>
調用getWindowReference()方法,然後返回Render()方法的結果。 讓我們檢查getWindowReference()來源:>>> 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
此方法正在返回對象的WindowReference屬性,正如我們從上面的LS -AL命令看到的那樣,它是Acmepatternsdecoratorsimplewindow的實例。 當然,我們本來可以研究DecoratedWindow :: __ construct()的工作方式,但這是我們可以檢查的另一種方式。
>>> 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_.*/ >>>
不幸的是,不支持通過像Apache這樣的Web服務器進行調試。但是,我們可以使用PHP的嵌入式服務器調試應用程序:
我們可以像我們對Cli
<span>>>> $a = 'hello'; </span><span>=> "hello" </span><span>>>></span>
進行單位測試調試
>>> function say($a) { ... echo $a; ... } => null >>> say('hello'); hello => null >>>
作為一個好的開發人員,您應該為代碼編寫單元測試,以證明其正常工作。在項目的文件中,您會找到測試文件夾,如果安裝了PHPUNIT,則可以在其內部運行測試。
>>>> 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" >>>
>我們具有生成錯誤的測試,文件和行。 讓我們看一下標題為windowtest.php
>>> ls $foo Class Methods: getA, setA >>>
如果您不熟悉phpunit,請不要過分關注該代碼。 簡而言之,我正在設置所有內容,以測試標題window :: addtitle()方法,並期望收到一個非空價值。
>>> ls -la $foo Class Properties: $a "hello" Class Methods: getA public function getA() setA public function setA($a)
>那麼,我們如何使用psysh檢查發生了什麼?只需像以前一樣添加shell :: debug()方法。
>{ "name": "example/psysh", "authors": [ { "name": "John Doe", "email": "john@doe.tst" } ], "require": { "psy/psysh": "~0.1" }, "autoload": { "psr-4": {"Acme\": "src/"} } }
我們準備好搖滾了!
>因此,在$ rs中,我們應該有一個字符串;讓我們看看我們真正擁有的。
<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>
> null值,難怪測試失敗了……讓我們檢查標題Window :: AddTitle()的源代碼。 如果我們執行LS命令,我們可以看到我們可以通過$ titledwindow對象獲得該對象的方法。
php public/decorator.php Simple Window +-------------+ | | | | | | | | | | +-------------+ Decorated Simple Window +-------------+ | | | | | | | | | | +-------------+ Titled Simple Window +-------------+ |Title | +-------------+ | | | | | | | | | | +-------------+
php -a Interactive shell php > $a = 'Hello world!'; php > echo $a; Hello world! php >
有錯誤。該方法是迴盪值而不是返回值。 即使該應用程序似乎可以正常工作,通過單元測試和PSYSH,我們發現了一個缺陷,現在我們可以修復它。
>本文並不是要詳盡地展示所有潛在的PSYS所具有的。 您應該嘗試其他一些很酷的功能(例如“ doc”)。 僅PSYS可能不是很有用,但是如果與其他工具和您的聰明調試功能相結合,則可以證明是寶貴的資產。
>常見問題(常見問題解答)關於與psysh>
我可以使用psysh在PHP中進行單位測試?是的,PSYSH對於PHP中的單位測試非常有用。您可以使用PSYS在測試執行過程中的任何時刻進行交互調試,檢查變量和狀態。這對於理解為什麼測試失敗可能特別有用。>
>官方PSYS網站及其GitHub存儲庫是找到資源的最佳場所關於psysh。它們包括詳細的文檔,用法示例和可用命令列表。您還可以在各種PHP和開發人員博客上找到教程和文章。以上是互動php與psysh調試的詳細內容。更多資訊請關注PHP中文網其他相關文章!