是上午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中文网其他相关文章!