Table of Contents
相遇
安装使用之
最后
Home Backend Development PHP Tutorial 使用VarDumper进行优雅的PHP调试

使用VarDumper进行优雅的PHP调试

Jun 23, 2016 pm 01:29 PM

原文来自: https://jellybool.com/post/a-brand-new-way-to-test-php-with-symfony-va...

相信很多PHP开发者在写代码的时候都会经常用到var_dump()这个函数,很多人都会直接用类似die(var_dump($var))来查看一个变量或者一个实例到底是长什么样的,稍微有一些人可能还直接封装过:比如直接叫一个vdd()等,以便于自己在调试自己的代码的时候使用。这种方式一直陪伴着我走过了这么久的编程时光,以至于造成了对var_dump()出来的现实样式都有一点审美疲劳了:因为var_dump()出来的可以说是完全没有什么美感啊,至少对于像我们这些代码工作者来说:你竟然没有高亮!!不能接受。

相遇

然后之前苦于没有找到很好的解决方案,也就是一直这样忍受着过来了,直到昨天我发现了这货:

Symfony VarDumper

测试样式是长这样的:

我第一眼看到这个的时候就马上爱上这货了,忍不住要写点东西来分享一下:

先来说说Symfony VarDumper的优点,Symfony VarDumper不仅可以做到像var_dump()一样调试,而且可以做得更好,并不是只靠脸生活的:

  • 你可以轻松配置输出数据的格式:HTML 或者 命令行样式

  • 对于一些可能重复太多的数据,VarDumper智能过滤将其折叠起来,并且你可以很完美地看到你的数据的结构是什么样的,不清楚的话等下可以看下面的截图。

  • 每个打印出来的对象或变量都有特定的样式。

  • 安装使用之

    说了这么多之后,我们终于要来一睹庐山真面目了。首先是安装,最简单的方法就是直接使用composer安装,创建一个新的文件夹php/,我们来测试一下:

    cd php/composer require symfony/var-dumper
    Copy after login

    再来创建一个index.php,将自动加载文件autoload.php包含进来:

    <?phprequire __DIR__.'/vendor/autoload.php';
    Copy after login

    首先在index.php写一个简单的数组来测试一下:

    <?phprequire __DIR__.'/vendor/autoload.php';$var = array(    'a simple string' => 'in an array of 5 elements',    'a float' => 1.0,    'an integer' => 1,    'a boolean' => true,    'an empty array' => array(),);dump($var);
    Copy after login

    出来的结果是这样的:

    有没有觉得很不错!这里还要说一点的是:如果你觉得Symfony VarDumper自带的样式不够美观,你可以直接到Dumper/HtmlDumper.php去修改你的自己的样式,比如你很喜欢github风,你完全可以自己在这个文件里面写你自己的css样式。

    上面对于数组的表现Symfony VarDumper貌似做得很完美,不仅给我们舒适的高亮,还很清晰的给了我们这个数组的结构。那么对于php中的stdObject,Symfony VarDumper的表现会是如何呢?我们来看看:

    class Test {    public $prop1 = 10;    private $prop2 = 20;    protected $prop3 = 30;    private $prop4 = 40;    public function __construct($value) {        $this->undefinedProp = $value;    }}$test = new Test(50);dump($test);
    Copy after login

    出来的结果是这样的,注意它的高粱颜色有不一样了:

    这里可以看到:public就用 + 表示,private 就用 - 表示,而protected 就用 # 表示。不见如此,如果你仔细看图,你会看到当鼠标浮在对应的属性上面的时候,会有一个小小的提示框来提醒我们这个具体是什么,很完美啊。

    我们既然需要测试,那么在类中添加对应的方法呢,这个到底会给我们什么样的调试反馈呢?

    class Test {    public $methodOne;    protected $methodTwo;    public function __construct() {        $this->methodTwo = function() {            return 'I am method 2';        };    }    public function buildFunction() {        $this->methodThree = function() {            return 'I am method 3';        };    }    public function __call($method, $args)    {        if (isset($this->$method)) {            $func = $this->$method;            return call_user_func_array($func, $args);        }    }}$test = new Test();$methodOne = function() {    return 'I am method 1';};$test->methodOne = $methodOne;$test->buildFunction();$test->methodOne();dump($test);
    Copy after login

    表现依然很惊艳:

    在上图中,你不仅可以很清晰地知道各个方法的类名是什么,也可以知道this代表的是什么,甚至还可以知道这个代码段是从第几行开始第几行结束的!666...

    最后

    可能很多同学看了这篇文章之后会觉得我们在自定义样式时直接改文件不太好,因为这个时候,如果你切换到其他的项目,你还是得重新再安装一次,难道还得再改一次?不是这样的,其实我推荐大家的做法是:全局安装Symfony VarDumper,这样不仅可以解决样式一次性问题,还可以让你在任何项目中使用Symfony VarDumper,安装方法如下:

    第一步,全局安装:

    composer global require symfony/var-dumper;
    Copy after login

    第二:配置php.ini

    在php.ini中找到auto_prepend_file,然后写上你相对应的路径,比如像下面这样的:

     auto_prepend_file = ${HOME}/.composer/vendor/autoload.php 
    Copy after login

    最后,更新composer

    直接命令行执行:

    composer global update
    Copy after login

    到这里,你就可以配置好一个很优雅的调试界面了。反正我是很喜欢,不知道你是什么感受。

    Happy Hacking

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1659
    14
    PHP Tutorial
    1258
    29
    C# Tutorial
    1232
    24
    How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

    Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

    Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

    There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

    PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

    PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

    What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

    HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

    Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

    In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

    PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

    PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

    Explain Arrow Functions (short closures) introduced in PHP 7.4. Explain Arrow Functions (short closures) introduced in PHP 7.4. Apr 06, 2025 am 12:01 AM

    The arrow function was introduced in PHP7.4 and is a simplified form of short closures. 1) They are defined using the => operator, omitting function and use keywords. 2) The arrow function automatically captures the current scope variable without the use keyword. 3) They are often used in callback functions and short calculations to improve code simplicity and readability.

    PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

    PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

    See all articles