3v4l.org

run code in 300+ PHP versions simultaneously
<?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 ' 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: ', $bear, ' Fish:', $fish;
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
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: 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 ) ) ) Fish:Array ( [id] => 1 [bear_id] => Array ( [id] => 1 [name] => A ) [name] => fish_a )
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/JmWfm on line 20
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/JmWfm 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/JmWfm on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/JmWfm 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/JmWfm on line 5
Process exited with code 255.

preferences:
329.76 ms | 401 KiB | 422 Q