Last active
February 12, 2016 11:35
-
-
Save useless-stuff/c752019c5da66224b046 to your computer and use it in GitHub Desktop.
PHP - Countable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class EmployeesCollection | |
*/ | |
class EmployeesCollection implements Countable | |
{ | |
protected $employees = array(); | |
/** | |
* Count elements of an object | |
* @link http://php.net/manual/en/countable.count.php | |
* @return int The custom count as an integer. | |
* </p> | |
* <p> | |
* The return value is cast to an integer. | |
* @since 5.1.0 | |
*/ | |
public function count() | |
{ | |
return count($this->employees); | |
} | |
/** | |
* @param Employee $employee | |
*/ | |
public function addEmployee(\Employee $employee) | |
{ | |
$this->employees[] = $employee; | |
} | |
} | |
/** | |
* Class Employee | |
*/ | |
class Employee | |
{ | |
protected $id; | |
/** | |
* @return mixed | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* @param mixed $id | |
*/ | |
public function setId($id) | |
{ | |
$this->id = $id; | |
} | |
} | |
$employeesCollection = new EmployeesCollection(); | |
for ($i = 1; $i < 51; $i++) { | |
$employee = new Employee(); | |
$employee->setId($i); | |
$employeesCollection->addEmployee($employee); | |
} | |
print_r($employeesCollection->count()); | |
// Output: | |
// 50 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment