2012-11-21 7 views
7

基本的に私はコードイグナイターを使用しています。コードイグナイターの基底クラスは、print_rオブジェクトの中に基本クラスが埋め込まれているものがたくさんあります。これは私が実際に望んでいた情報(残りのプロパティ)を取得するのに苦労します。プライベートプロパティをprint_rまたはobjectから除外しますか?

私は隠すことができるか、基本クラスオブジェクトを削除する方法があるのだろうかと思っていますか?

私は

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

を試してみましたが、当然のCI施設には、プライベートです。

私はダンプのために使用しています実際の機能は次のとおりです。

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

答えて

3

これは、未知のクラスの唯一の公共VARSを返すために働く必要があります。

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

そして、これは非常にハックですが、作品 - へオブジェクトからプライベートプロパティを削除します(オブジェクト名は保持されません)。もちろん、プロパティ名をハードコードする必要があるという別の欠点もあります。

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

をしかし、私は他のプライベートプロパティをしたい、私はちょうど1つのプロパティ – Hailwood

+0

@Hailwoodが、今ちょっとハックの方法を試してみたが、可能性があることを望んでいません作業。 – arma

+0

しかし、 'get_object_vars()'はオブジェクトをパラメータとして想定しています。 '$ class_of_object'は文字列です。 – TheFox

1

あなたが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); 
    } 
} 

方法についても同じことで簡単で行うことができます。

本当に必要な場合は、特別な機能を作成してこの作業を行い、そのような分析が必要なときに呼び出すことができます。しかし、私は、ほとんどの場合、私のお気に入りのPHPStormのような良いIDEは、あなたとクラスインスタンスを呼び出すときにこの仕事をするかもしれないと思う - 提案リストの公開メソッドだけを表示する。

+0

私はまた、PHPStormがあなたのためにこれを行う方法の情報を追加するためにPHPStormケアを使用しますか? – Hailwood

+0

さて、$ obj - > [ctrl + Enter]とタイプすると、プライベートメソッドやプロパティは表示されません。 – Pavel

0

私は私のために正常に動作します簡単な方法があります。

<?php print_r(json_decode(json_encode($object))); ?> 
関連する問題