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 git.master, git.master_jit, rfc.property-hooks
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 )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
42.7 ms | 409 KiB | 8 Q