views:

32

answers:

2

Hi,

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

+1  A: 

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.

Jon Cram
A: 

Yes, but only internally. It's just a matter of not exposing the relevant property in the hash table returned by the get_properties handler.

You can also hide the property behind a __get handler, but you still have to store the data somewhere and __get has a performance (and clarity) penalty.

Artefacto