Is there a ToStringBuilder for php?

To quickly write good toString() methods in PHP, I am looking for something like the ToStringBuilder class for java.

Update : I am not asking about how to use or implement toString() . I am asking for a powerful helper like ToStringBuilder for java. ToStringBuilder is a big time ToStringBuilder because it can define many things by reflection on its own (for example, collections, array, nested classes).

+4
source share
2 answers

PHP does not need ToStringBuilder ; In almost all cases, a simple print_r() or var_dump() will perform the task of resetting any variable (including objects) for debugging purposes. To get a more accurate and reproducible representation, you can use var_export() . For instance:

 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 a string representation instead of printing their output. var_dump() does not have this function, 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 the Java implementation, this is a very minimalistic port for PHP, which I whipped:

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

Here's how you could use it in your 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] 

Serialization is not very accurate, because Booleans will be represented either by an empty string, or simply 1 . It can be improved :)

+3
source

Here is a brief implementation, similar to the one you linked, but a little different. The result is very similar to var_export (but not quite the same) and spans several 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")); 
+1
source

All Articles