Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PHP, can a "var_dump" be done on an object without including the dump of objects set to its properties?

Tags:

oop

php

PHP's "var_dump" function outputs recursively an object's properties. I was wondering if there is a way to "dump" an object, but do not dump recursive objects within the original object.

Original Dump:

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) {
    }
  }
}

Wanted Dump:

object(Class_Name)#1 (3) {
  ["label":protected]=>
  string(16) "My Label"
  ["name":protected]=>
  string(16) "name"
  ["object":protected]=>
  object(Class_Name)#2 (2) { ... }
}
like image 363
Andrew Bessa Avatar asked May 23 '14 15:05

Andrew Bessa


2 Answers

You could write your Own

/**
 * 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;
    }
}
like image 161
Christian Gollhardt Avatar answered Oct 27 '22 00:10

Christian Gollhardt


Something along these lines should do the trick :

<?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());

prints :

Dump of object of class : Foo
Methods :
array(1) {
  [0]=>
  string(6) "do_foo"
}
Properties :
var1 : (NULL)  
var2 : (NULL)  
like image 27
Loïc Avatar answered Oct 26 '22 22:10

Loïc