How to print object values in php?

To get more information use this custom TO[$someObject] function:

I wrote this simple function which not only displays the methods of a given object, but also shows its properties, encapsulation and some other useful information like release notes if given.

function TO[$object]{ //Test Object
                if[!is_object[$object]]{
                    throw new Exception["This is not a Object"]; 
                    return;
                }
                if[class_exists[get_class[$object], true]] echo "
CLASS NAME = ".get_class[$object];
                $reflection = new ReflectionClass[get_class[$object]];
                echo "
"; echo $reflection->getDocComment[]; echo "
"; $metody = $reflection->getMethods[]; foreach[$metody as $key => $value]{ echo "
". $value; } echo "
"; $vars = $reflection->getProperties[]; foreach[$vars as $key => $value]{ echo "
". $value; } echo "
"; }

To show you how it works I will create now some random example class. Lets create class called Person and lets place some release notes just above the class declaration:

        /**
         * DocNotes -  This is description of this class if given else it will display false
         */
        class Person{
            private $name;
            private $dob;
            private $height;
            private $weight;
            private static $num;

            function __construct[$dbo, $height, $weight, $name] {
                $this->dob = $dbo;
                $this->height = [integer]$height;
                $this->weight = [integer]$weight;
                $this->name = $name;
                self::$num++;

            }
            public function eat[$var="", $sar=""]{
                echo $var;
            }
            public function potrzeba[$var =""]{
                return $var;
            }
        }

Now lets create a instance of a Person and wrap it with our function.

    $Wictor = new Person["27.04.1987", 170, 70, "Wictor"];
    TO[$Wictor];

This will output information about the class name, parameters and methods including encapsulation information and the number of parameters, names of parameters for each method, method location and lines of code where it exists. See the output below:

CLASS NAME = Person
/**
             * DocNotes -  This is description of this class if given else it will display false
             */

Method [  public method __construct ] {
  @@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 75 - 82

  - Parameters [4] {
    Parameter #0 [  $dbo ]
    Parameter #1 [  $height ]
    Parameter #2 [  $weight ]
    Parameter #3 [  $name ]
  }
}

Method [  public method eat ] {
  @@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 83 - 85

  - Parameters [2] {
    Parameter #0 [  $var = '' ]
    Parameter #1 [  $sar = '' ]
  }
}

Method [  public method potrzeba ] {
  @@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 86 - 88

  - Parameters [1] {
    Parameter #0 [  $var = '' ]
  }
}


Property [  private $name ]

Property [  private $dob ]

Property [  private $height ]

Property [  private $weight ]

Property [ private static $num ]

[PHP 4, PHP 5, PHP 7, PHP 8]

print_r Prints human-readable information about a variable

Description

print_r[mixed $value, bool $return = false]: string|bool

print_r[], var_dump[] and var_export[] will also show protected and private properties of objects. Static class members will not be shown.

Parameters

value

The expression to be printed.

return

If you would like to capture the output of print_r[], use the return parameter. When this parameter is set to true, print_r[] will return the information rather than print it.

Return Values

If given a string, int or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements. Similar notation is used for objects.

When the return parameter is true, this function will return a string. Otherwise, the return value is true.

Examples

Example #1 print_r[] example



The above example will output:

Array
[
    [a] => apple
    [b] => banana
    [c] => Array
        [
            [0] => x
            [1] => y
            [2] => z
        ]
]

Example #2 return parameter example

Notes

Note:

When the return parameter is used, this function uses internal output buffering prior to PHP 7.1.0, so it cannot be used inside an ob_start[] callback function.

See Also

  • ob_start[] - Turn on output buffering
  • var_dump[] - Dumps information about a variable
  • var_export[] - Outputs or returns a parsable string representation of a variable

liamtoh6 at hotmail dot com

12 years ago

I add this function to the global scope on just about every project I do, it makes reading the output of print_r[] in a browser infinitely easier.



It also makes sense in some cases to add an if statement to only display the output in certain scenarios, such as:

if[debug==true]
if[$_SERVER['REMOTE_ADDR'] == '127.0.0.1']

simivar at gmail dot com

5 years ago

I've fixed function wrote by Matt to reverse print_r - it had problems with null values. Created a GIST for that too so please add any future fixes in there instead of this comment section:
//gist.github.com/simivar/037b13a9bbd53ae5a092d8f6d9828bc3

$val] {
    if [
is_numeric[$key]] $key = "arr_".$key; //

Bob

13 years ago

Here is a function that formats the output of print_r as a expandable/collapsable tree list using HTML and JavaScript.

Chủ Đề