2012-11-21 15 views
7

Fondamentalmente sto usando Code Igniter e la classe base Code Igniter è enorme, quando stampo alcuni dei miei oggetti hanno la classe base incorporata al loro interno. questo rende difficile ottenere le informazioni che effettivamente desidero (il resto delle proprietà).esclude la proprietà privata da print_r o oggetto?

Quindi, mi chiedo se c'è un modo per nascondere o rimuovere l'oggetto della classe base?

ho cercato

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

ma ovviamente la proprietà c'è privato.

la funzione reale che sto usando per il 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'); 
} 

risposta

3

questo dovrebbe funzionare per la restituzione di Vars solo pubbliche per la classe sconosciuta:

// 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); 

E questo è abbastanza hacky, ma funziona - a rimuovere la proprietà privata dall'oggetto (Ciò non preserverà il nome dell'oggetto) e naturalmente un altro inconveniente è che è necessario il nome della proprietà del codice hardware:

// 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); 
+0

Ma voglio che le altre proprietà private, solo che non voglio che una proprietà – Hailwood

+0

@Hailwood provare ora, un po 'modo hacky, ma potrebbe lavoro. – arma

+0

Ma 'get_object_vars()' si aspetta un oggetto come parametro. '$ class_of_object' è una stringa. – TheFox

1

si può fare in facilmente con PHP reflextion API

$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); 
    } 
} 

La stessa cosa per i metodi.

Se proprio ne hai bisogno, puoi creare una funzione speciale per farlo funzionare e chiamarlo quando hai bisogno di tale analisi. Ma penso che nella maggior parte dei casi un buon IDE, come il mio PHPStorm preferito, potrebbe fare questo lavoro per te e quando chiami l'istanza di classe - mostra solo metodi pubblici nell'elenco di suggerimenti.

+0

Io uso anche PHPStorm per aggiungere informazioni su come PHPStorm fa questo per te? – Hailwood

+0

Bene, quando si digita $ obj -> [ctrl + Invio] semplicemente non mostra metodi o proprietà private. – Pavel

0

ho un modo semplice che funziona bene per me:

<?php print_r(json_decode(json_encode($object))); ?> 
Problemi correlati