The debugInfo() magic method is useful in customizing the way an object is dumped out in a class.

class DevPeel
{
	protected $name = 'devpeel';
}

$class = new DevPeel();
var_dump($class);

That’s the simple way of really simple debugging, now let’s see how can we customize it with __debugInfo() magic method.

Another Example

class DevPeel
{
	protected $name = 'devpeel';
	protected $data = [
		'email'=>'contact@devpeel.com'
	];

	public function __debugInfo(){
		return $this->data;
	}
}

$class = new DevPeel();
var_dump($class);

Here we’ve used __debugInfo() magic method and it lets us to only show our desired dataset, in our case its $this->data. Its’ a lot easier way to examine large data collection in our development environment where we need to debug specific items only.

You may also Like