搜索
首页后端开发php教程互动php与psysh调试

互动php与psysh调试

是上午1:00,您的Web应用程序的交付截止日期为8小时……而且行不通。 当您尝试弄清楚发生了什么时,您将代码填充var_dump()和die()到处都在查看错误在哪里…

>

>你很生气。每次您想尝试返回值或变量分配时,都必须更改源代码,执行应用程序并查看结果……最终,您不确定是否已将所有这些var_dump从代码。这种情况很熟悉吗?

>

钥匙要点

    PSYSH是PHP的功能强大的REPL工具,可以通过允许立即进行互动和执行PHP代码来增强调试,类似于浏览器中的JavaScript控制台。 PSYS的安装可以在全球或使用Composer进行全球完成,并且支持在运行时检查和操纵代码的一系列命令。> 通过使用``ls',show'''和`help'之类的命令,开发人员可以检查变量,查看方法定义并直接在控制台中获取有关代码的详细信息。
  • > PSYS可以直接集成到PHP脚本或单元测试中,以提供实时调试环境,这对于识别和修复复杂应用程序中的错误特别有用。
  • >该工具在命令行接口和内置的PHP Web服务器中提供了无缝调试体验,尽管它与Apache等外部Web服务器不兼容。
  • >
  • > psysh进行救援
  • PSYSH是一个读取的印刷循环(或repl)。 您可能在通过浏览器的JavaScript控制台之前使用了一个卧式。 如果有的话,您知道它具有很大的力量,并且在调试JS代码时可能会很有用。
  • >谈论PHP,您之前可能已经使用过PHP的Interactive Console(PHP -A)。在那里,您可以编写一些代码,并且一旦按Enter:,该控制台将立即执行它
  • 不幸的是,交互式外壳不是一个替补,因为它缺少“ p”(打印)。 我必须执行回声语句才能查看$ a的内容。 在真实的补充中,我们将在将值分配给它后立即看到。
  • >
>您可以在Composer g需要的情况下在全球安装PSYSH,或者下载PSYSH可执行

作曲家

>直接下载(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 >
>我定义了say()并调用它。 您看到的这两个null是因为函数定义和执行都没有返回值(函数回荡值)。 此外,在定义功能时,提示从>>>更改为....

>

我们可以定义类并实例化吗?

composer g require psy/psysh:~0.1
psysh
>当我实例化foo时,构造函数返回对对象的引用。 这就是为什么Psysh打印。 现在让我们看看关于psysh和对象的有趣的东西。

>

wget psysh.org/psysh
chmod +x psysh
./psysh
如果您忘记了类Foo定义的哪种方法,则您现在有了答案。 您是否使用过Linux OS或Mac命令行接口?那么您可能会熟悉LS命令。 还记得-la选项吗?

./psysh                                                                                                                                             

Psy Shell v0.1.11 (PHP 5.5.8 — cli) by Justin Hileman                                                                                                                                                                              
>>>
甜,不是吗?

与Web应用程序集成时,PSYS的真实力量会闪耀,所以让我们构建一个。

演示应用

>我将实施一个快速应用程序来展示装饰器设计模式。这种模式的UML类图如下:


>如果您对UML或设计模式不了解,则不必担心本文不需要理解它们。 互动php与psysh调试

>也为此项目创建了一组测试用例。 这些测试用例可以由Phpunit运行。同样,您不必熟悉单元测试就可以理解本文。

>

可以在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
请查看来自文件public/decorator.php的以下源代码。它将实例化简单的窗口,装饰窗户和标题为窗户的对象,以展示装饰器图案:

我们可以通过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_.*/
 >>>
在CLI

中调试 通过命令行界面执行上述代码的执行方式将如下所示:

>

>我们如何与psysh互动? 只需添加psyshell :: debug(get_defined_vars());您要调试应用程序的代码上的任何位置,通常在哪里插入var_dump()语句:

>

<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
so,$ window是SimpleWindow的一个实例,它实现了窗口界面……我想知道窗口界面的源代码看起来像…

>

wget psysh.org/psysh
chmod +x psysh
./psysh
>为什么简单的窗口和装饰窗口具有相同的输出?让我们检查一下$ DecoratedWindow对象。

>

./psysh                                                                                                                                             

Psy Shell v0.1.11 (PHP 5.5.8 — cli) by Justin Hileman                                                                                                                                                                              
>>>
这个对象比简单的窗口“重”,因此源代码可能很长……让我们仅查看render()方法的源代码:

>

调用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的嵌入式服务器调试应用程序:

开发服务器现在正在聆听端口8080上的连接,因此,一旦我们通过此Web服务器(https:// localhost:8080/decorator.php)请求Decorator.php文件,我们就应该看到以下内容:

我们可以像我们对Cli
<span>>>> $a = 'hello';
</span><span>=> "hello"
</span><span>>>></span>
>一样开始玩PSYSH

进行单位测试调试
>>> 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> {}
>>> $foo->setA('hello');
=> null
>>> $foo->getA();
=> "hello"
>>></foo>

即使代码似乎完美地运行,测试也会失败。我们可以通过仅运行失败测试来进一步检查:

>我们具有生成错误的测试,文件和行。 让我们看一下标题为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></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

进行交互式PHP调试

什么是psysh,它如何在PHP调试中起作用?它提供了一个交互式命令行接口,您可以在其中执行PHP代码并立即查看输出。 PSYS对于调试特别有用,因为它允许您逐步浏览代码,检查变量并进行交互式测试更改。这就像与您的代码进行对话一样,这可能会导致更好地理解和更快的错误分辨率。

>如何安装PSYSH进行php调试?

psysh可以使用作曲家,A PHP的依赖关系管理工具。您可以通过运行Command Composer Global需要PSY/PSYS进行安装。安装后,您可以简单地在终端中键入psysh来启动PSYS。确保将全局作曲家二进制文件包括在您的路径中,以便您的系统可以找到PSYSH可执行文件。

>我如何使用psysh调试我的php代码?

,您可以插入psysh();在您的代码中的任何时候,您都想开始交互式调试会话。当您的代码执行达到这一点时,PSYS将打开一个交互式外壳,允许您检查变量,执行代码并逐步浏览您的代码执行。

>

我可以使用psysh在PHP中进行单位测试?是的,PSYSH对于PHP中的单位测试非常有用。您可以使用PSYS在测试执行过程中的任何时刻进行交互调试,检查变量和状态。这对于理解为什么测试失败可能特别有用。

>

我如何自定义PSYSH配置?

psysh允许您通过在您的.psysh.php中创建.psysh.php文件来自定义其配置。主目录。在此文件中,您可以设置配置选项,例如默认值包括,错误级别和命令历史记录大小。您还可以添加自定义命令或清洁剂。

>

psysh的一些高级功能是什么?

psysh具有许多高级功能,可以帮助您更有效地调试PHP代码。其中包括使用运行时执行代码,自动分号插入,名称空间支持,读取线支持,异常处理等。 PSYS还支持变量,功能,类,甚至PHP内置关键字的选项卡完成。

psysh如何处理错误和异常?

psysh具有强大的错误和异常处理机制。当发生错误或异常时,PSYSH将显示详细的堆栈跟踪,帮助您确切地了解错误发生的位置和原因。您还可以在任何时候使用WTF命令来显示最后一个异常堆栈跟踪。

>我可以将psysh与我喜欢的php框架一起使用吗?

是的,PSYSH可以很好地与大多数PHP框架,大多数PHP框架,包括Laravel,Symfony和Zend框架。一些框架,例如Laravel,甚至包括PSYSH开箱即用。您可以通过报告错误,建议功能或提交拉力请求来为该项目做出贡献。在贡献之前,请确保阅读该项目的贡献指南。

>

>我在哪里可以找到更多资源来了解PSYSH?

>

>官方PSYS网站及其GitHub存储库是找到资源的最佳场所关于psysh。它们包括详细的文档,用法示例和可用命令列表。您还可以在各种PHP和开发人员博客上找到教程和文章。

>

以上是互动php与psysh调试的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
unset()和session_destroy()有什么区别?unset()和session_destroy()有什么区别?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

在负载平衡的情况下,什么是粘性会话(会话亲和力)?在负载平衡的情况下,什么是粘性会话(会话亲和力)?May 04, 2025 am 12:16 AM

stickysessensureuserRequestSarerOutedTothesMeServerForsessionDataConsisterency.1)sessionIdentificeAssificationAssigeaSsignAssignSignSuserServerServerSustersusiseCookiesorUrlModifications.2)一致的ententRoutingDirectSsssssubsequeSssubsequeSubsequestrequestSameSameserver.3)loadBellankingDisteributesNebutesneNewuserEreNevuseRe.3)

PHP中有哪些不同的会话保存处理程序?PHP中有哪些不同的会话保存处理程序?May 04, 2025 am 12:14 AM

phpoffersvarioussessionsionsavehandlers:1)文件:默认,简单的ButMayBottLeneckonHigh-trafficsites.2)Memcached:高性能,Idealforsforspeed-Criticalapplications.3)REDIS:redis:similartomemememememcached,withddeddeddedpassistence.4)withddeddedpassistence.4)databases:gelifforcontrati forforcontrati,有用

PHP中的会话是什么?为什么使用它们?PHP中的会话是什么?为什么使用它们?May 04, 2025 am 12:12 AM

PHP中的session是用于在服务器端保存用户数据以在多个请求之间保持状态的机制。具体来说,1)session通过session_start()函数启动,并通过$_SESSION超级全局数组存储和读取数据;2)session数据默认存储在服务器的临时文件中,但可通过数据库或内存存储优化;3)使用session可以实现用户登录状态跟踪和购物车管理等功能;4)需要注意session的安全传输和性能优化,以确保应用的安全性和效率。

说明PHP会话的生命周期。说明PHP会话的生命周期。May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

绝对会话超时有什么区别?绝对会话超时有什么区别?May 03, 2025 am 12:21 AM

绝对会话超时从会话创建时开始计时,闲置会话超时则从用户无操作时开始计时。绝对会话超时适用于需要严格控制会话生命周期的场景,如金融应用;闲置会话超时适合希望用户长时间保持会话活跃的应用,如社交媒体。

如果会话在服务器上不起作用,您将采取什么步骤?如果会话在服务器上不起作用,您将采取什么步骤?May 03, 2025 am 12:19 AM

服务器会话失效可以通过以下步骤解决:1.检查服务器配置,确保会话设置正确。2.验证客户端cookies,确认浏览器支持并正确发送。3.检查会话存储服务,如Redis,确保其正常运行。4.审查应用代码,确保会话逻辑正确。通过这些步骤,可以有效诊断和修复会话问题,提升用户体验。

session_start()函数的意义是什么?session_start()函数的意义是什么?May 03, 2025 am 12:18 AM

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)