Use Vardumper for elegant PHP debugging

Source: Internet
Author: User
Tags autoload

Original from: Https://jellybool.com/post/a-brand-new-way-to-test-php-with-symfony-va ...

It is believed that many PHP developers often use the Var_dump () function when writing code, and many people will use Die (Var_dump ($var)) to see what a variable or an instance looks like, Some people may also be directly encapsulated: For example, call a VDD (), etc., so that they can be used when debugging their own code. This kind of way has been accompanying me for so long the programming time, so that caused the var_dump () out of the realistic style has a little aesthetic fatigue: because Var_dump () out of the can be said to be completely no sense of beauty ah, at least for the code workers like us: you did not highlight!! Not acceptable.

Meet

And then I didn't find a good solution, that is to endure it, until yesterday I found the goods:

Symfony Vardumper

The test style is long like this:

When I first saw this, I immediately fell in love with the goods, and could not help but write something to share:

First of all, the advantages of Symfony Vardumper, Symfony Vardumper can not only do like Var_dump () debugging, and can do better, not only by the face of life:

  • You can easily configure the format of the output data: HTML or command line style

  • For some of the data that may be repeated too much, vardumper smart filter folds it up, and you can perfectly see what the structure of your data is, and what you can see below.

  • Each printed object or variable has a specific style.

  • Installation and use of

    After saying so much, we are finally going to have a glimpse of the truth. First is the installation, the simplest way is to directly use the composer installation, create a new folder php/, let us test:

    CD Php/composer require Symfony/var-dumper

    Then create a index.php that will automatically load the file autoload.php included in:

       

    First, write a simple array in index.php to test:

     
       ' In an array of 5 elements ',    ' a float ' + = 1.0,    ' an integer ' = = 1,    ' a Boolean ' = = True,    ' an empty Array ' = = Array (),);d UMP ($VAR);

    The result is this:

    Did not feel very good! Here's another point: if you think the style of Symfony Vardumper is not beautiful enough, you can go directly to dumper/ htmldumper.php to modify your own style, such as you like the GitHub wind, you can write your own CSS style in this file.

    The above for the performance of the array symfony Vardumper seems to be perfect, not only to give us a comfortable highlight, but also very clear to our structure of this array. What about the performance of Stdobject,symfony Vardumper in PHP? Let's take a look at:

    Class Test {public    $prop 1 = ten;    Private $prop 2 =;    protected $prop 3 =;    Private $prop 4 = +;    Public function __construct ($value) {        $this->undefinedprop = $value;    }} $test = new test;d UMP ($test);

    Out of the result is this, note that its sorghum color has not the same:

    As you can see here: public is denoted by +, private is used-to represent, and protected is denoted by #. Not so, if you look closely at the picture, you will see that when the mouse floats on the corresponding property, there will be a small hint box to remind us of this specific is what is perfect ah.

    Since we need to test, then add the corresponding method in the class, what kind of debugging feedback will this give us?

    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 ();d UMP ($test);

    The performance is still stunning:

    In, you can not only clearly know the class name of each method, you can also know what this represents, and even know that this code snippet from the beginning of the first few lines to the end of the line! 666 ...

    At last

    Maybe a lot of students read this article will feel that we are in the custom style of the file is not very good, because this time, if you switch to other projects, you still have to re-install again, do you have to change again? Not so, in fact, I recommend the practice is: the global installation of Symfony Vardumper, so not only to solve the style of a one-time problem, but also allows you to use any project Symfony Vardumper, the installation method is as follows:

    The first step, the global installation:

    Composer Global require symfony/var-dumper;

    Second: Configure PHP.ini

    Find the Auto_prepend_file in the php.ini and write your corresponding path, such as the following:

    Finally, update composer

    Direct command line execution:

    Composer Global Update

    Here, you can configure a very elegant debugging interface. Anyway, I like it very much, I don't know how you feel.

    Happy Hacking

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.