Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide specific class fields from print_r or var_dump

Tags:

php

Is it possible to hide a specific class fields from print_r ?

<?php

class DataManager {
    public $data = array();
}

class Data {
    public $manager;
    public $data = array();

    public function Data ($m, $d) {
        $this->manager = $m;
        $this->data = $d;
    }
}

$manager = new DataManager();

for ($a = 0; $a < 10; $a++) {
    $manager->data[] = new Data($manager, 'Test ' . md5($a));
}

echo '<pre>';
print_r($manager);

?>

This would print

DataManager Object ( [data] => Array ( [0] => Data Object ( [manager] => DataManager Object RECURSION [data] => Test cfcd208495d565ef66e7dff9f98764da )

        [1] => Data Object
            (
                [manager] => DataManager Object  *RECURSION*
                [data] => Test c4ca4238a0b923820dcc509a6f75849b
            )    .......

Is it possible to somehow change the output behavior so it print's like this? Like with DocComment /** @hidden **/

DataManager Object ( [data] => Array ( [0] => Data Object ( [data] => Test cfcd208495d565ef66e7dff9f98764da )

        [1] => Data Object
            (
                [data] => Test c4ca4238a0b923820dcc509a6f75849b
            )

If not, is there some sort of PHP lib that maybe uses Reflection and somehow bypasses stuff?

Thanks

like image 518
Angelo Vargas Avatar asked Jul 09 '10 20:07

Angelo Vargas


People also ask

What is the difference between Var_dump () and Print_r ()?

var_dump() displays values along with data types as output. print_r() displays only value as output. It does not have any return type. It will return a value that is in string format.

Why Var_dump () is preferable over Print_r ()?

It's too simple. The var_dump() function displays structured information about variables/expressions including its type and value. Whereas The print_r() displays information about a variable in a way that's readable by humans. Example: Say we have got the following array and we want to display its contents.


2 Answers

New Magic method __debugInfo() was introduced in PHP 5.6 that will allow you to modify the default behaviour of var_dump() when dumping your objects.

Have a look at the documentation.

Example:

<?php
class C {
    private $prop;

    public function __construct($val) {
        $this->prop = $val;
    }

    public function __debugInfo() {
        return [
            'propSquared' => $this->prop ** 2,
        ];
    }
}

var_dump(new C(42));
?>

Returns:

object(C)#1 (1) {
  ["propSquared"]=>
  int(1764)
}

Although this question is 4 years old, I'm sure someone will find this useful in the future.

like image 114
Dušan Brejka Avatar answered Oct 18 '22 20:10

Dušan Brejka


Both print_r() and var_dump() will give you everything.

Various Reflection classes have a getDocComment() method to get the /** doc comment */ for classes, methods and properties.

Utilising doc comments to denote what should and should not be output, you can quite easily create a dumping class to achieve what you want.

like image 36
Jon Cram Avatar answered Oct 18 '22 20:10

Jon Cram