<?php
class Node implements Countable
{
private $nodes = [];
public function __construct(array $nodes = [])
{
$this->nodes = $nodes;
}
public function getCount()
{
return count($this->nodes);
}
public function getCountRecursive()
{
$count = 0;
foreach ($this->nodes as $node) {
$count += ($node->getCountRecursive() + 1);
}
return $count;
}
public function count($mode)
{
if ($mode === COUNT_RECURSIVE) {
return $this->getCountRecursive();
}
return $this->getCount();
}
}
$node = new Node([
new Node([
new Node([
new Node([
new Node(),
new Node(),
new Node(),
]),
new Node(),
new Node(),
]),
new Node(),
new Node(),
]),
new Node(),
]);
var_dump(count($node), count($node, COUNT_RECURSIVE));