3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class Model { protected $id; public function __construct($id = 0) { $this->id = $id; } public function __toString() { return print_r($this->generate(), true); } } class Bear extends Model { protected $relations = [ 'hasOne' => [ ['Fish', 'id', 'bear_id'] ] ]; public $data = [ ['id' => 1, 'name' => 'A'], ['id' => 2, 'name' => 'B'], ['id' => 3, 'name' => 'C'], ['id' => 4, 'name' => 'D'] ]; public function generate() { $relation = $this->relations['hasOne'][0]; $name = $relation[0]; $first = $relation[1]; $second = $relation[2]; $relation = (new $name)->data; $bear = $this->data[$this->id]; foreach ($relation as $key => $value) { if ($bear[$first] === $value[$second]) { $bear[$name][] = $value; } } return print_r($bear, true); } } class Fish extends Model { protected $relations = [ 'belongsTo' => [ ['Bear', 'bear_id', 'id'] ] ]; public $data = [ ['id' => 1, 'bear_id' => 1, 'name' => 'fish_a'], ['id' => 2, 'bear_id' => 2, 'name' => 'fish_b'], ['id' => 3, 'bear_id' => 3, 'name' => 'fish_c'], ['id' => 4, 'bear_id' => 1, 'name' => 'fish_d'] ]; public function generate() { $relation = $this->relations['belongsTo'][0]; $name = $relation[0]; $first = $relation[1]; $second = $relation[2]; $relation = (new $name)->data; $fish = $this->data[$this->id]; foreach ($relation as $key => $value) { if ($fish[$first] === $value[$second]) { $fish[$first] = $value; } } return print_r($fish, true); } } $bear = new Bear(0); $fish = new Fish(0); echo ' <meta charset="utf-8"> <pre> Database `bears` Database `fishes` ┌────────────┬──────────────┐ ┌────────────┬──────────────┬──────────────┐ │ id │ name │ │ id │ bear_id │ name │ ├────────────┼──────────────┤ ├────────────┼──────────────┼──────────────┤ │ 1 │ A │ │ 1 │ 1 │ fish_a │ ├────────────┼──────────────┤ ├────────────┼──────────────┼──────────────┤ │ 2 │ B │ │ 2 │ 2 │ fish_b │ ├────────────┼──────────────┤ ├────────────┼──────────────┼──────────────┤ │ 3 │ C │ │ 3 │ 3 │ fish_c │ ├────────────┼──────────────┤ ├────────────┼──────────────┼──────────────┤ │ 4 │ D │ │ 4 │ 1 │ fish_d │ └────────────┴──────────────┘ └────────────┴──────────────┴──────────────┘ ', $bear, '<hr>', $fish;
Output for 5.4.0 - 7.1.0
<meta charset="utf-8"> <pre> Database `bears` Database `fishes` ┌────────────┬──────────────┐ ┌────────────┬──────────────┬──────────────┐ │ id │ name │ │ id │ bear_id │ name │ ├────────────┼──────────────┤ ├────────────┼──────────────┼──────────────┤ │ 1 │ A │ │ 1 │ 1 │ fish_a │ ├────────────┼──────────────┤ ├────────────┼──────────────┼──────────────┤ │ 2 │ B │ │ 2 │ 2 │ fish_b │ ├────────────┼──────────────┤ ├────────────┼──────────────┼──────────────┤ │ 3 │ C │ │ 3 │ 3 │ fish_c │ ├────────────┼──────────────┤ ├────────────┼──────────────┼──────────────┤ │ 4 │ D │ │ 4 │ 1 │ fish_d │ └────────────┴──────────────┘ └────────────┴──────────────┴──────────────┘ Array ( [id] => 1 [name] => A [Fish] => Array ( [0] => Array ( [id] => 1 [bear_id] => 1 [name] => fish_a ) [1] => Array ( [id] => 4 [bear_id] => 1 [name] => fish_d ) ) ) <hr>Array ( [id] => 1 [bear_id] => Array ( [id] => 1 [name] => A ) [name] => fish_a )
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/KrHSn on line 20
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/KrHSn on line 20
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/KrHSn on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/KrHSn on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/KrHSn on line 5
Process exited with code 255.