2012-11-21 8 views
7

Zasadniczo używam Code Igniter, a podstawowa klasa Code Igniter jest ogromna, gdy drukuję niektóre z moich obiektów, mają wbudowaną klasę podstawową. to sprawia, że ​​trudno jest uzyskać informacje, które faktycznie chciałem (pozostałe właściwości).wykluczyć własność prywatną z print_r lub obiektu?

Zastanawiam się, czy istnieje sposób, w jaki mogę się ukryć, czy usunąć obiekt klasy bazowej?

Próbowałem

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

ale oczywiście właściwość ci jest prywatny.

rzeczywista funkcja używam do dumpingu jest:

/** 
* 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'); 
} 

Odpowiedz

3

To powinno działać na powrocie vars tylko publiczne dla nieznanego Klasa:

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

I to jest całkiem hacky, ale działa - do usuń własność prywatną z obiektu (nie zachowa ona nazwy obiektu) i oczywiście inną wadą jest to, że musisz podać nazwę właściwości twardego kodu:

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

ale chcę inne własności prywatnej, ja po prostu nie chce, że jedna właściwość – Hailwood

+0

@Hailwood spróbować teraz, z klasą hacky sposób, ale może praca. – arma

+0

Ale 'get_object_vars()' oczekuje obiektu jako parametru. '$ class_of_object' to ciąg znaków. – TheFox

1

Można to zrobić w prosty sposób z 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); 
    } 
} 

samo dla metod.

Jeśli naprawdę tego potrzebujesz - możesz utworzyć specjalną funkcję, która wykona tę pracę za ciebie i zadzwonisz do niej, gdy będziesz potrzebować takiej analizy. Ale myślę, że w większości przypadków dobry IDE, taki jak mój ulubiony PHPStorm, może wykonać tę pracę dla ciebie i kiedy zadzwonisz do instancji klasy - pokaż tylko publiczne metody na liście sugestii.

+0

Używam również PHPStorm care, aby dodać informacje o tym, jak PHPStorm robi to za Ciebie? – Hailwood

+0

Cóż, jeśli typeing $ obj -> [Ctrl + Enter] po prostu nie pokazują prywatnych metod lub właściwości. – Pavel

0

mam prosty sposób, który działa dobrze dla mnie:

<?php print_r(json_decode(json_encode($object))); ?> 
Powiązane problemy