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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With