Each major PHP release adds a number of new features, some of which really matter. For PHP 5.3, these were namespaces and anonymous functions. For PHP 5.4 - traits. For PHP 5.5 - generators. For 5.6, variable-length argument lists.class Address { protected $street; protected $city; protected $state; protected $zip; public function __construct($street, $city, $state, $zip) { $this->street = $street; $this->city = $city; $this->state = $state; $this->zip = $zip; } public function getStreet() { return $this->street; } public function getCity() { return $this->city; } public function getState() { return $this->state; } public function getZip() { return $this->zip; } } class Employee { protected $address; public function __construct(Address $address) { $this->address = $address; } public function getAddress() : Address { return $this->address; } } $a = new Address('123 Main St.', 'Chicago', 'IL', '60614'); $e = new Employee($a); print $e->getAddress()->getStreet() . PHP_EOL; // Prints 123 Main St. Employee object, which has only one property that contains the email address we passed. Notice the getAddress() method. After the parameters of the function, we have a colon and a type. It is the only type that can take a return value.getAddress() method, PHP will throw a TypeError exception. Even null will not satisfy type requirements. This allows us to refer to print to the method of the Address object with absolute confidence. We will know for sure that an object of this type is indeed returned, not null , not false, not a string, or some other object. This is what ensures the safety of work and the absence of the need for additional checks, which in turn makes our own code cleaner. Even if something goes wrong, PHP will definitely warn us.Address object? We introduce the EmployeeRepository , the logic of which allows you to have no records. First we add the ID field to the Employee class: class Employee { protected $id; protected $address; public function __construct($id, Address $address) { $this->id = $id; $this->address = $address; } public function getAddress() : Address { return $this->address; } } class EmployeeRepository { private $data = []; public function __construct() { $this->data[123] = new Employee(123, new Address('123 Main St.', 'Chicago', 'IL', '60614')); $this->data[456] = new Employee(456, new Address('45 Hull St', 'Boston', 'MA', '02113')); } public function findById($id) : Employee { return $this->data[$id]; } } $r = new EmployeeRepository(); print $r->findById(123)->getAddress()->getStreet() . PHP_EOL; interface AddressInterface { public function getStreet(); public function getCity(); public function getState(); public function getZip(); } class EmptyAddress implements AddressInterface { public function getStreet() { return ''; } public function getCity() { return ''; } public function getState() { return ''; } public function getZip() { return ''; } } class Address implements AddressInterface { // ... } class Employee { // ... public function getAddress() : AddressInterface { return $this->address; } } class EmployeeRepository { // ... public function findById($id) : Employee { if (!isset($this->data[$id])) { throw new InvalidArgumentException('No such Employee: ' . $id); } return $this->data[$id]; } } try { print $r->findById(123)->getAddress()->getStreet() . PHP_EOL; print $r->findById(789)->getAddress()->getStreet() . PHP_EOL; } catch (InvalidArgumentException $e) { print $e->getMessage() . PHP_EOL; } /* * Prints: * 123 Main St. * No such Employee: 789 */ getStreet() will return a nice empty value.Source: https://habr.com/ru/post/267257/
All Articles