3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Thing { public function __construct( public readonly string $name, public readonly ?Thing $left = null, public readonly ?Thing $right = null, ) { } public function hasThings(): bool { return $this->left || $this->right; } public function getThingDepth(): int{ if(!$this->hasThings()) { return 1; } return max($this->left->getThingDepth(), $this->right->getThingDepth()) + 1; } public function getHtml(): string { $buf = []; $buf[] = '<table>'; $buf[] = '<tr>'; $buf[] = '<td>'; $buf[] = $this->name; $buf[] = '</td>'; if($this->hasThings()){ $buf[] = '<td>'; $buf[] = $this?->left->getHtml() ?? '-'; $buf[] = $this?->right->getHtml() ?? '-'; $buf[] = '</td>'; } $buf[] = '</tr>'; $buf[] = '</table>'; return implode("\n", $buf); } } $me = new Thing( 'Child', new Thing('Dad', new Thing('Dad\'s dad'), new Thing('Dad\'s mom')), new Thing('Mom', new Thing('Mom\'s dad'), new Thing('Mom\'s mom')), ); echo $me->getHtml();

preferences:
67.61 ms | 409 KiB | 6 Q