Is there any functions or how to var_dump() object without showing it protected and private property?
example:
class foo {
public $public = 'public';
protected $protected = 'protected';
private $private = 'private';
}
$Foo = new foo;
var_dump($Foo);
// Expected output "(string) public"
json_encode
will only encode public properties.
As this page shows, you can just loop over the object:
<?php
class person {
public $FirstName = "Bill";
public $MiddleName = "Terence";
public $LastName = "Murphy";
private $Password = "Poppy";
public $Age = 29;
public $HomeTown = "Edinburgh";
public $FavouriteColour = "Purple";
}
$bill = new person();
foreach($bill as $var => $value) {
echo "$var is $value\n";
}
?>
Note that the $Password variable is nowhere in sight, because it is marked Private and we're trying to access it from the global scope.
If you want to make your own var dump, you can do it as so:
function dumpObj( $obj )
{
foreach( $obj as $k=>$v )
{
echo $k . ' : ' . $v ."\n";
}
}
dumpObj( new WhateverClass() );
The reason this works is because when you access the object outside of itself, you only have access to its public facing member variables.
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