Use VarDumper for elegant PHP Debugging
From: https://jellybool.com/post/a-brand-new-way-to-test-php-with-symfony-va...
I believe that many PHP developers often use the var_dump () function when writing code. many people will directly use a function similar to die (var_dump ($ var )) to check what a variable or instance looks like. some people may also encapsulate it directly, for example, a vdd, this allows you to use it when debugging your code. This method has always accompanied me through so long programming time that it has caused a little aesthetic fatigue for the practical style var_dump (): because var_dump () it can be said that there is no beauty at all, at least for code workers like us: you have not highlighted it !! Unacceptable.
Encounter
Then I failed to find a good solution, that is, I had to endure it until yesterday I found the product:
Symfony VarDumper
The test style is as follows:
When I first saw this, I immediately fell in love with this product. I couldn't help writing something to share with me:
First, let's talk about the advantages of Symfony VarDumper. Symfony VarDumper can not only debug like var_dump (), but also do better, not just face-to-face:
You can easily configure the output data format: HTML or command line style
VarDumper intelligently filters out data that may be too large, and you can see exactly what the structure of your data is, if you have no idea, you can wait and see the following.
Each printed object or variable has a specific style.
Install and use
After talking so much about it, we finally want to see what it looks like. First, install it. The simplest method is to directly install it using composer and create a new folder php/. let's test it:
cd php/composer require symfony/var-dumper
Create an index. php file to include the automatically loaded file autoload. php:
First, write a simple array in index. php to test it:
'in an array of 5 elements', 'a float' => 1.0, 'an integer' => 1, 'a boolean' => true, 'an empty array' => array(),);dump($var);
The result is as follows:
Do you think it's good! One thing to mention here is: if you think the style in Symfony VarDumper is not beautiful enough, you can directly go to Dumper/HtmlDumper. php modifies your own style. for example, if you like github, you can write your own css style in this file.
The above expression of the array Symfony VarDumper seems to have done a perfect job, not only giving us a comfortable highlight, but also clearly giving us the structure of this array. So what is the performance of stdObject and Symfony VarDumper in php? Let's take a look:
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);
The result is as follows:
Here we can see that public is represented by +, private is represented by-, and protected is represented. If you look at the picture carefully, you will see a small prompt box to remind us of what this is, which is perfect.
Now that we need to test the function, how can we add the corresponding method to the class? what 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();dump($test);
The performance is still amazing:
In, you can not only clearly know the class names of each method, but also what this represents, you can even know that this code segment ends from the first line! 666...
LastAfter reading this article, many people may think that it is not good for us to directly change the file in the custom style, because at this time, if you switch to another project, you still have to reinstall it. do you have to change it again? In fact, we recommend that you install Symfony VarDumper globally. This not only solves one-time style problems, but also allows you to use Symfony VarDumper in any project, the installation method is as follows:
Step 1: Global installation:
composer global require symfony/var-dumper;
Second: configure php. ini
Find auto_prepend_file in php. ini and write your corresponding path, such as the following:
auto_prepend_file = ${HOME}/.composer/vendor/autoload.php
Finally, update composer.
Run the following command:
composer global update
Now, you can configure a very elegant debugging interface. I like it very much. I don't know how you feel.
Happy Hacking