Exclude private property from print_r or object?

I mainly use Code Igniter, and the Code Igniter base class is huge when I print some of my objects, they have a base class built into them. it makes the pain get the information that I really wanted (other properties).

So, I am wondering if there is a way to hide or remove the base class object?

I tried

clone $object; unset($object->ci); print_r($object); 

but, of course, the ci property is private.

actual function i use for dumping:

 /** * Outputs the given variables with formatting and location. Huge props * out to Phil Sturgeon for this one (http://philsturgeon.co.uk/blog/2010/09/power-dump-php-applications). * To use, pass in any number of variables as arguments. * Optional pass in "true" as final argument to kill script after dump * * @return void */ function dump() { list($callee) = debug_backtrace(); $arguments = func_get_args(); $total_arguments = count($arguments); if (end($arguments) === true) $total_arguments--; echo '<fieldset style="background: #fefefe !important; border:2px red solid; padding:5px">'; echo '<legend style="background:lightgrey; padding:5px;">' . $callee['file'] . ' @ line: ' . $callee['line'] . '</legend><pre>'; $i = 0; foreach ($arguments as $argument) { //if the last argument is true we don't want to display it. if ($i == ($total_arguments) && $argument === true) break; echo '<br/><strong>Debug #' . (++$i) . ' of ' . $total_arguments . '</strong>: '; if ((is_array($argument) || is_object($argument)) && count($argument)) { print_r($argument); } else { var_dump($argument); } } echo '</pre>' . PHP_EOL; echo '</fieldset>' . PHP_EOL; //if the very last argument is "true" then die if (end($arguments) === true) die('Killing Script'); } 
+7
source share
3 answers

This should work to return only public vars for an unknown class:

 // Get the class of the object $class_of_object= get_class($object); // Get only public attributes (Note: if called from within class will return also private and protected) $clone = get_class_vars($class_of_object); // Try it dump($clone); 

And this is pretty hacky, but it works - to remove private property from the object (this will not save the name of the object), and, of course, another drawback is that you need to specify the name of the hard code property:

 // First cast clone to array $b = (array) clone($a); // Then unset value (There will be null bytes around A and to use them you need to run it in double quotes // Replace A for * to remove protected properties unset($b["\0A\0_ci"]); // Finally back to object $b = (object) $b; // Test it dump($b); 
+3
source

You can easily with PHP API Reflextion

 $myClassName = 'myChildClass'; $reflection = new ReflectionClass($myClassName); // get properties, only public in this case $properties = $reflection->getProperties(ReflectionMethod::IS_PUBLIC); //Print all properties (including parent class) print_r($properties); //Print properties of desired class only foreach ($properties as $property) { if ($property->class == $myClassName) { print_r($property); } } 

The same goes for the methods.

If you really need it - you can create a special function to do this work for you and call it when you need such an analysis. But I think that in most cases, a good IDE, like my favorite PHPStorm, can do the job for you, and when you call an instance of the class, show only public methods in the list of sentences.

+1
source

I have a simple way that works great for me:

 <?php print_r(json_decode(json_encode($object))); ?> 
0
source

All Articles