When we use the var_dump
without the method __debugInfo
will be displayed at the output the data, including the private and protected variables, as cited in the documentation:
This method is called by var_dump()
when dumping an Object to get the properties that should be Shown. If the method isn’t defined on an Object, then all public, protected and private properties will be Shown.
Then the __debugInfo
will be used to limit what will be displayed on output, an 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 way out will be something like:
object(Foo)#1 (1) {
["private::podeIrParaoLog"]=>
string(4) "Olá"
}
Foo Object
(
[private::podeIrParaoLog] => Olá
)
I cost understanding this method, in all my tests it doesn’t work. Even with your example it didn’t work.
– Boi Programador
@Boyprogrammer you are right, most people do blog posts or even answers in other places that are not as direct as they should be and another time they do the opposite, are too direct when they should be more detailed before presenting the code part, explaining necessary technical concepts. One of the reasons I strive to formulate long and other short answers is to analyze when one type and when another is necessary, because I also penetrated to learn because of bad texts that I found on the Internet, so I try to do better to help.
– Guilherme Nascimento
That’s why I tested positive, I can tell you know what you’re talking about and you know what you’re saying. But thank you for the answer, now that I saw that it is very old. But it was good for study. Thank you
– Boi Programador