Using __debugInfo()
The __debugInfo() magic method gets triggered when the var_dump() function is called. By default, the var_dump() function shows all public, protected, and private properties of an object. However, if an object class implements the __debugInfo() magic method, we get to control the output of the var_dump() function. The method does not accept any parameters, and returns an array of key-values to be shown, as per the following synopsis:
array __debugInfo(void)
The following example demonstrates the __debugInfo() method implementation:
<?php
class User
{
public $name = 'John';
public $age = 34;
private $salary = 4200.00;
private $bonus = 680.00;
protected $identifier = 'ABC';
protected $logins = 67;
public function __debugInfo()
{
return [
'name' => $this->name,
'income' => $this->salary + $this->bonus
];
}
}
$user = new User();
var_dump($user);This results in the following output:
object(User)#1 (2) {
["name"]=>...