3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class ClientController { private $collection; public function __construct($collection) { $this->collection = $collection; $this->validate(); } protected function validate() { foreach ($this->collection as $model) { $props = get_object_vars($model); foreach ($props as $key => $val) { $method = 'validate' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))); $valid = $model->$method(); if (!$valid) { $model->addError($key); } } if (length($model->getErrors())) { $model->setValid(false); } } } public function getCollection() { return $this->collection; } } class ClientModel { private $name; private $cpf; private $birth_date; private $email; private static $is_valid = true; private static $errors = []; 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 validateName() { return (!empty($this->name) && !preg_match("/^[a-zA-Z'-]+$/", $this->name)); } public function getCpf() { return $this->cpf; } public function setCpf($cpf) { $this->cpf = $cpf; } public function validateCpf() { return (!empty($this->cpf) && ctype_digit($this->cpf)); } public function getBirthDate() { return $this->birth_date; } public function setBirthDate($birth_date) { $this->birth_date = $birth_date; } public function validateBirthDate() { return (!empty($this->birth_date) && preg_match("/^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/", $this->birth_date)); } public function getEmail() { return $this->email; } public function setEmail($email) { $this->email = $email; } public function validateEmail() { return (filter_var($this->email, FILTER_VALIDATE_EMAIL)); } public function isValid() { return $this->is_valid; } public function setValid($valid) { $this->is_valid = $valid; } public function addError($field) { $this->errors[] = $field; } public function getErrors() { return $this->errors; } } 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); } $ctrl = new ClientController($collection); var_dump($ctrl->getCollection());
based on LpnMv
Output for 5.4.0 - 7.2.0
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.3.29
Parse error: syntax error, unexpected '[' in /in/DbUDj on line 40
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/DbUDj on line 40
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/DbUDj on line 6
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/DbUDj on line 6
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/DbUDj on line 6
Process exited with code 255.