Why use __debugInfo ()?

4

I saw PHP 5.6 now create the new magic method, called __debugInfo .

This method is intended to return a array , so it will be printed on the var_dump call. This array returned obviously should provide debugging information for the class.

I would like to understand in what cases this would be useful?

Was not the traditional% by itself enough enough? Was this done just to solve circular reference problems?

    
asked by anonymous 17.11.2016 / 14:51

1 answer

3

When we use var_dump without the __debugInfo method the data, including private and protected variables, will be displayed in the output, as the documentation cites:

  

This method is called by var_dump() when dumping an object to get the properties that should be shown. If the method is not defined on an object, then all public, protected and private properties will be shown.

Then% w / o will be used to limit what will appear in the output, for example:

<?php
class Foo
{
    private $podeIrParaoLog = 'Olá';
    private $naoPodeIrParaoLog = 'Informação sensivel';

    public function __debugInfo()
    {
        return [
            'private::podeIrParaoLog' => $this->podeIrParaoLog,
        ];
    }
}

$foo = new Foo();

var_dump($foo);

print_r($foo);

The output will look something like:

object(Foo)#1 (1) {
  ["private::podeIrParaoLog"]=>
  string(4) "Olá"
}
Foo Object
(
    [private::podeIrParaoLog] => Olá
)
    
17.11.2016 / 15:13