BetaRide
BetaRide

Reputation: 16844

Is there a ToStringBuilder for php?

In order to quickly write nice toString() methods in PHP I'm looking for something like the ToStringBuilder class for java.

Update: I'm not asking about how to use or implement toString(). I'm asking about a powerfull helper like ToStringBuilder for java. ToStringBuilder is a big time saver because it can figure out a lot of things by reflection by itself (e.g. collections, array, nested classes).

Upvotes: 4

Views: 288

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173562

PHP doesn't really have a need for ToStringBuilder; in nearly all cases a simple print_r() or var_dump() will do the job of dumping any variable (including objects) for the purpose of debugging. To get a more exact and reproducible representation, you can use var_export(). For example:

class Test {
    private $x = 'hello';
    protected $y = 'world';
}

$t = new Test;

var_export($t);
var_dump($t);
print_r($t);

Output:

Test::__set_state(array(
   'x' => 'hello',
   'y' => 'world',
))

object(Test)#1 (2) {
  ["x":"Test":private]=>
  string(5) "hello"
  ["y":protected]=>
  string(5) "world"
}

Test Object
(
    [x:Test:private] => hello
    [y:protected] => world
)

Both var_export() and print_r() take a second parameter to return the string representation instead of printing their output. var_dump() doesn't have this feature, so you need another way:

ob_start();
var_dump($t);
$s = ob_get_clean();

echo "Object is $s\n";

See also: print_r() var_dump() var_export()

Old answer

From my understanding of Java's implementation, this is a very minimalistic port to PHP I whipped up:

class ToStringBuilder
{
    private $o; // the object to build a string for
    private $h; // a hash of parameters

    public function __construct($obj)
    {
        $this->o = $obj;
        $this->h = array();
    }

    public function append($key, $value)
    {
        $this->h[] = "$key=$value";
        // you could also use this for a full representation of the PHP type
        // "$key=" . var_export($value, true)
        return $this; // make it chainable
    }

    // final serialization
    public function toString()
    {
        return get_class($this->o) . '@' . spl_object_hash($this->o) .
            '[' . join(',', $this->h) . ']';
    }

    // help method to avoid a separate $x = new ToStringBuilder() statement
    public function getInstance($obj)
    {
        return new self($obj);
    }
}

This is how you would use it in your own class:

class Person
{
    private $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function __toString()
    {
        // use ToStringBuilder to form a respresentation
        return ToStringBuilder::getInstance($this)
            ->append('name', $this->name)
            ->toString();
    }
}

$person = new Person("John Doe");
echo $person;
// Person@00000000708ab57c0000000074a48d4e[name=John Doe]

The serialization isn't very exact, because booleans will be represented either with an empty string or just 1. This could be improved :)

Upvotes: 3

Alvin Wong
Alvin Wong

Reputation: 12420

Here is a quick implementation, similar to the one you've linked but slightly different. The output closely resembles var_export (but not exactly the same) and span multiple lines:

<?php
class ToStringBuilder{
    private $str;

    public function __construct($my){
        $this->str = get_class($my) . "(\n";
    }

    public static function create($my){
        return new ToStringBuilder($my);
    }

    public function append($name, $var){
        $this->str .= "  " . $name . " => " . str_replace("\n", "\n  ", var_export($var, true)) . ",\n";
        return $this;
    }

    public function __toString(){
        return $this->str . ")";
    }
}

Demo: http://codepad.org/9FJvpODp


To use it, refer to the following code:

class Person{
    private $name;
    private $age;
    private $hobbies;

    public function __toString(){
        return strval(ToStringBuilder::create($this)
                      ->append("name", $this->name)
                      ->append("age", $this->age)
                      ->append("hobbies", $this->hobbies)
                     );
    }

    public function __construct($name, $age, $hobbies){
        $this->name = $name;
        $this->age = $age;
        $this->hobbies = $hobbies;
    }
}

echo new Person("hello", 18, array("swimming", "hiking"));

Upvotes: 1

Related Questions