3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class ToStringMadness { private $data; public function __construct($data) { $this->data = $data; } public function __toString() { //$data = array_map(function($item) { return $item; }, $this->data); $data = array_merge($data); sort($data); $rs = implode('-', $data); echo $rs."\n"; return $rs; } } $a = [ new ToStringMadness(['c','b']), new ToStringMadness(['c','b','a']), new ToStringMadness(['a','b']), ]; sort($a); print_r($a); usort($a, function(array $itemA, array $itemB) { return strcmp((string)$itemA, (string)$itemB); });
based on LTMJ0
Output for 7.0.0 - 7.2.0
Array ( [0] => ToStringMadness Object ( [data:ToStringMadness:private] => Array ( [0] => a [1] => b ) ) [1] => ToStringMadness Object ( [data:ToStringMadness:private] => Array ( [0] => c [1] => b ) ) [2] => ToStringMadness Object ( [data:ToStringMadness:private] => Array ( [0] => c [1] => b [2] => a ) ) ) Fatal error: Uncaught TypeError: Argument 1 passed to {closure}() must be of the type array, object given in /in/K6YHT:31 Stack trace: #0 [internal function]: {closure}(Object(ToStringMadness), Object(ToStringMadness)) #1 /in/K6YHT(33): usort(Array, Object(Closure)) #2 {main} thrown in /in/K6YHT on line 31
Process exited with code 255.
Output for 5.4.9 - 5.6.28
Array ( [0] => ToStringMadness Object ( [data:ToStringMadness:private] => Array ( [0] => a [1] => b ) ) [1] => ToStringMadness Object ( [data:ToStringMadness:private] => Array ( [0] => c [1] => b ) ) [2] => ToStringMadness Object ( [data:ToStringMadness:private] => Array ( [0] => c [1] => b [2] => a ) ) ) Catchable fatal error: Argument 1 passed to {closure}() must be of the type array, object given in /in/K6YHT on line 31
Process exited with code 255.
Output for 5.3.19 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/K6YHT on line 21
Process exited with code 255.