Skip to content

Instantly share code, notes, and snippets.

@useless-stuff
Last active February 12, 2016 11:35
Show Gist options
  • Save useless-stuff/c752019c5da66224b046 to your computer and use it in GitHub Desktop.
Save useless-stuff/c752019c5da66224b046 to your computer and use it in GitHub Desktop.
PHP - Countable
<?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