2014-05-23 9 views
5

La funzione "var_dump" di PHP restituisce in modo ricorsivo le proprietà di un oggetto. Mi chiedevo se esiste un modo per "scaricare" un oggetto, ma non scaricare oggetti ricorsivi all'interno dell'oggetto originale.In PHP, un "var_dump" può essere eseguito su un oggetto senza includere il dump di oggetti impostato sulle sue proprietà?

Dump originale:

object(Class_Name)#1 (3) { 
    ["label":protected]=> 
    string(16) "My Label" 
    ["name":protected]=> 
    string(16) "name" 
    ["object":protected]=> 
    object(Class_Name)#2 (2) { 
    ["id":protected]=> 
    NULL 
    ["classes":protected]=> 
    array(0) { 
    } 
    } 
} 

Dump Ricercato:

object(Class_Name)#1 (3) { 
    ["label":protected]=> 
    string(16) "My Label" 
    ["name":protected]=> 
    string(16) "name" 
    ["object":protected]=> 
    object(Class_Name)#2 (2) { ... } 
} 
+2

dovreste scrivere voi stessi. var_dump/print_r sono per il debugging e salvano TUTTO. se vuoi meno, dovrai farlo da solo. –

+1

Vedere l'estensione xDebug o provare qualcosa come var_dump (json_decode (json_encode ($ v))) per sbarazzarsi degli oggetti – DarkSide

risposta

5

Si potrebbe scrivere il proprio

/** 
* Schows all visible Vars of a Object, only in the first Level. 
* To get private or protected we need to call the class 
* in the Context of the Object ($this) 
* @param object $obj The Object 
* @param string|null $newLineCharacter The New Line Character (If null, it is based on \n for CLI or <br/> on web 
* @return void 
*/ 
function firstLevelVarDump($obj, $newLineCharacter = null) { 
    //Decide which new Line Character we use (Based on Loïc suggestion) 
    if ($newLineCharacter === null) { 
     $newLineCharacter = php_sapi_name() == 'cli' ? PHP_EOL : '<br/>'; 
    } 
    //Get all visible Items 
    $data = get_object_vars($obj); 

    //Loop through each Item 
    foreach ($data as $key => $item) { 
     //Display Key + Type 
     echo $key . ' => ' . gettype($item); 

     //Extract Details, beased on the Type 
     if (is_string($item)) { 
      echo '(' . strlen($item) . ') "' . $item . '"'; 
     } elseif (is_bool($item)) { 
      echo '(' . ($item ? 'true' : 'false') . ')'; 
     } elseif (is_integer($item) || is_float($item)) { 
      echo '(' . $item . ')'; 
     } elseif (is_object($item)) { 
      echo '(' . get_class($item) . ')'; 
     } 

     //Line Break 
     echo $newLineCharacter; 
    } 
} 
+1

'echo" \ n "' funziona bene su cli e unix, ma non per il web, vedere la mia risposta sotto per usare qualcosa di più appropriato ('$ EOL = php_sapi_name() == 'cli'? PHP_EOL: '
';') –

+0

Grazie per il commento. È un buon punto. Ma var_dump usa anche '\ n'. Ad ogni modo aggiorno la mia risposta. –

1

Qualcosa in questo senso dovrebbe fare il trucco:

<?php 

function object_dump($object, $show_methods = true){ 
    $EOL = php_sapi_name() == 'cli' ? PHP_EOL : '<br/>'; 
    $LS = php_sapi_name() == 'cli' ? '--------------------------'.PHP_EOL : '<hr/>'; 
    if(php_sapi_name() != 'cli'){echo "<pre>";} 

    echo "Dump of object of class : ".get_class($object).$EOL.$LS; 

    if($show_methods){ 
     echo "Methods :".$EOL; 
     var_dump(get_class_methods($object)); 
     echo $LS; 
    } 

    echo "Properties :" . $EOL; 

    foreach(get_object_vars($object) as $property => $value){ 
     if(gettype($value) == "object"){ 
      $value = "object of ".get_class($value); 
     } 
     echo "$property : (".gettype($value).") $value $EOL"; 
    } 

    if(php_sapi_name() != 'cli'){echo "</pre>";} 

} 


class Foo 
{ 
    public $var1; 
    public $var2; 

    function do_foo() 
    { 
     echo "Doing foo."; 
    } 

} 

object_dump(new Foo()); 

stampe:

Dump of object of class : Foo 
Methods : 
array(1) { 
    [0]=> 
    string(6) "do_foo" 
} 
Properties : 
var1 : (NULL) 
var2 : (NULL) 
Problemi correlati