3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * A simple user account database */ class UserAccountDatabase { private $data = []; /** * Save a user account * * @param string $username * @param string $password * @return void * @throws Exception */ public function saveAccount($username, $password) { if ($this->getAccount($username) !== null) { throw new \Exception('Username already exists in database.'); } $this->data[$username] = $password; } /** * Get a user account by username * * @param string $username * @return array|null */ public function getAccount($username) { return isset($this->data[$username]) ? $this->data[$username] : null; } /** * Delete a user account by username * * @param string $username * @return void */ public function deleteAccount($username) { unset($this->data[$username]); } } /** * You must implement this interface */ interface UserAccountManagerInterface { /** * @param UserAccountDatabase $db */ public function __construct(UserAccountDatabase $db); /** * Allow a user to create an account. Should return true if account creation * was successful and false otherwise. * * @param string $username * @param string $password * @return bool */ public function create($username, $password); /** * Allow a user to authenticate using the same username and password that * they used when creating their account. Should return true if the account * exists and the password matches or false otherwise. * * @param string $username * @param string $password * @return bool */ public function authenticate($username, $password); } class UserAccountManager implements UserAccountManagerInterface { private $db; public function __construct(UserAccountDatabase $db) { $this->db = $db; } public function create($username, $password) { try { $this->db->saveAccount($username, $password); return true; } catch (Exception $e) { return false; } } public function authenticate($username, $password) { $stored_pass = $this->db->getAccount($username); if ($stored_pass === null) { return false; } return $password === $stored_pass; } } /////////////////////////// $input = fopen("php://memory", "rw"); $data = '[ [ ["abe","12345"], ["ben","67890"], ["ben","67890"] ], [ ["abe","12345"], ["ben","67890"], ["ben","67890"], ["carla","12345"] ] ]'; fwrite($input, $data); fseek($input, 0); // Setup Streams //$input = fopen("php://stdin", "r"); $output = fopen("php://stdout","w"); // Input (JSON) $arguments = json_decode(trim(stream_get_contents($input)), true, 16); //var_dump($arguments); die(); $db = new UserAccountDatabase(); $manager = new UserAccountManager($db); $result = []; // Create Accounts foreach ($arguments[0] as $account) { $result[0][] = call_user_func_array([$manager, 'create'], $account); } // Authenticate Accounts foreach ($arguments[1] as $account) { $result[1][] = call_user_func_array([$manager, 'authenticate'], $account); } // Output (JSON) fwrite($output, json_encode($result)); fclose($output);
Output for git.master, git.master_jit, rfc.property-hooks
[[true,true,false],[true,true,true,false]]

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.71 ms | 401 KiB | 8 Q