3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ClientController { function __construct($clients) { foreach ($clients as $key => $value) { $clients[$key]['name_valid'] = $this->validName($value['name']); $clients[$key]['cpf_valid'] = $this->validCpf($value['cpf']); $clients[$key]['birth_date_valid'] = $this->validBirthdate($value['birth_date']); $clients[$key]['email_valid'] = $this->validEmail($value['email']); } print_r($clients); } function validName($name) { return (!empty($name) && !preg_match("/^[a-zA-Z'-]+$/", $name)); } function validCpf($cpf) { return (!empty($cpf) && ctype_digit($cpf)); } function validBirthdate($date) { return (!empty($birthdate) && preg_match("/^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/", $birthdate)); } function validEmail($email) { return (filter_var($email, FILTER_VALIDATE_EMAIL)); } } class ClientModel { private $name; private $cpf; private $birth_date; private $email; public function __construct($name, $cpf, $birth_date, $email) { $this->setName($name); $this->setCpf($cpf); $this->setBirthDate($birth_date); $this->setEmail($email); } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function getCpf() { return $this->cpf; } public function setCpf($cpf) { $this->cpf = $cpf; } public function getBirthDate() { return $this->birth_date; } public function setBirthDate($birth_date) { $this->birth_date = $birth_date; } public function getEmail() { return $this->email; } public function setEmail($email) { $this->email = $email; } } class ClientCollection { private $_clients = []; public function add(ClientModel $client) { $this->_clients[] = $client; end($this->_clients); return key($this->_clients); } public function get($key) { if (isset($this->_clients[$key])) { return $this->_clients[$key]; } else { throw new Exception("Client does not exist"); } } public function delete($key) { if (isset($this->_clients[$key])) { unset($this->_clients[$key]); } else { throw new Exception("Client does not exist"); } } } $clients = array( array("name" => "Hubert Adams", "cpf" => "86383860011", "birth_date" => "20/07/2015", "email" => "carlee_fadel@block.name"), array("name" => "Isabelle Mann", "cpf" => "58958058684", "birth_date" => "27/07/2015", "email" => "dortha.pacocha@ebert.biz"), array("name" => "Jayda Reichert", "cpf" => "01075384664", "birth_date" => "24/07/2015", "email" => "kaylie@stehrgaylord.biz"), array("name" => "Brionna Reinger", "cpf" => "48991299636", "birth_date" => "22/07/2015", "email" => "ansley.orn@herzog.org"), array("name" => "Diego", "cpf" => "55483741684aaaa", "birth_date" => "", "email" => "raoul mills com") ); $collection = new ClientCollection(); foreach ($clients as $client) { $model = new ClientModel($client['name'], $client['cpf'], $client['birth_date'], $client['email']); $collection->add($model); } var_dump($collection);
Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.21, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 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
object(ClientCollection)#1 (1) { ["_clients":"ClientCollection":private]=> array(5) { [0]=> object(ClientModel)#2 (4) { ["name":"ClientModel":private]=> string(12) "Hubert Adams" ["cpf":"ClientModel":private]=> string(11) "86383860011" ["birth_date":"ClientModel":private]=> string(10) "20/07/2015" ["email":"ClientModel":private]=> string(23) "carlee_fadel@block.name" } [1]=> object(ClientModel)#3 (4) { ["name":"ClientModel":private]=> string(13) "Isabelle Mann" ["cpf":"ClientModel":private]=> string(11) "58958058684" ["birth_date":"ClientModel":private]=> string(10) "27/07/2015" ["email":"ClientModel":private]=> string(24) "dortha.pacocha@ebert.biz" } [2]=> object(ClientModel)#4 (4) { ["name":"ClientModel":private]=> string(14) "Jayda Reichert" ["cpf":"ClientModel":private]=> string(11) "01075384664" ["birth_date":"ClientModel":private]=> string(10) "24/07/2015" ["email":"ClientModel":private]=> string(23) "kaylie@stehrgaylord.biz" } [3]=> object(ClientModel)#5 (4) { ["name":"ClientModel":private]=> string(15) "Brionna Reinger" ["cpf":"ClientModel":private]=> string(11) "48991299636" ["birth_date":"ClientModel":private]=> string(10) "22/07/2015" ["email":"ClientModel":private]=> string(21) "ansley.orn@herzog.org" } [4]=> object(ClientModel)#6 (4) { ["name":"ClientModel":private]=> string(5) "Diego" ["cpf":"ClientModel":private]=> string(15) "55483741684aaaa" ["birth_date":"ClientModel":private]=> string(0) "" ["email":"ClientModel":private]=> string(15) "raoul mills com" } } }
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/LpnMv on line 74
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/LpnMv on line 74
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/LpnMv on line 34
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/LpnMv on line 34
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/LpnMv on line 34
Process exited with code 255.

preferences:
276.98 ms | 401 KiB | 340 Q