3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Matrix { private int $rows; private int $columns; private array $values; public function __construct(int $rows, int $columns) { $this->rows = $rows; $this->columns = $columns; } public function rows(): int { return $this->rows; } public function columns() : int { return $this->columns; } } class MatrixFactory { public function create(int $rows, int $columns) : Matrix{ return new Matrix($rows, $columns); } } interface MatrixCommand { public function execute(Matrix $matrix): Response; public function title() :string; } class TransposeMatrixCommand implements MatrixCommand { public const TITLE = 'Pomnóż macierze wejściowe'; public function title() : string { return self::TITLE; } public function execute(Matrix $matrix):Response { return new Response(); } } class MultiplicationMatrixCommand implements MatrixCommand { public const TITLE = 'Pomnóż macierze wejściowe'; public function title() : string { return self::TITLE; } public function execute(Matrix $matrix):Response { return new Response(); } } class Response { } class RegistryMatrixCommand { private array $registry; public function __construct(iterable $commands) { $this->addCommands($commands); } public function addCommand(string $command) { if(false === class_exists($command)) { throw new Exception('Class not exists'); } $this->registry[$command] = new $command; } public function addCommands(iterable $commands) { foreach($commands as $command) { $this->addCommand($command); } } public function get(string $action) : MatrixCommand { if(false === array_key_exists($action, $this->registry)) { throw new Exception('Not found command!'); } return $this->registry[$action]; } public function list() : iterable { return $this->registry; } } class MenuRenderer { private array $commands; public function __construct(array $commands) { $this->commands = $commands; } } class MatrixAction { private RegistryMatrixCommand $registry; private MenuRenderer $menuRenderer; public function __construct(RegistryMatrixCommand $registry, MenuRenderer $menuRenderer) { $this->registry = $registry; $this->menuRenderer = $menuRenderer; } public function displayActions(): string { return $this->menuRenderer->render($this->registry->list()); } public function execute(Request $request) : Response { $matrix = (new MatrixFactory())->create( $request->rows(), $request->columns() ); $command = $this->registry->get($request->action()); $response = $command->execute($matrix); return $response; } } class Request { private string $action; private int $rows; private int $columns; public function __construct(string $action, int $rows, int $columns) { $this->action = $action; $this->rows = $rows; $this->columns = $columns; } public function action() :string { return $this->action; } public function rows() : int { return $this->rows; } public function columns(): int { return $this->columns; } } $actions = new ArrayIterator([ MultiplicationMatrixCommand::class, TransposeMatrixCommand::class ]); $registryMatrix = new RegistryMatrixCommand($actions); $menuRenderer = new MenuRenderer(); $matrixAction = new MatrixAction($registryMatrix, $menuRenderer); $request = new Request('TransposeMatrixCommand', 3, 4); echo $matrixAction->displayActions(); // var_dump($matrixAction->execute($request));
Output for git.master, git.master_jit, rfc.property-hooks
Fatal error: Uncaught ArgumentCountError: Too few arguments to function MenuRenderer::__construct(), 0 passed in /in/438kG on line 157 and exactly 1 expected in /in/438kG:97 Stack trace: #0 /in/438kG(157): MenuRenderer->__construct() #1 {main} thrown in /in/438kG on line 97
Process exited with code 255.

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:
43.06 ms | 401 KiB | 8 Q