<?php
class Head
{
public $mouth;
private function __construct()
{
$this->mouth = new Mouth();
}
private function hurts()
{
return [
'my ' . get_class($this) => 'ouch!'
];
}
public function recursive($painThreshold = 10)
{
$painThresholdMet = !$painThreshold;
if ($painThresholdMet) {
return $this->mouth;
}
$func = __FUNCTION__;
$pain = $this->hurts()['my Head'];
$this->mouth->speech .= "$func functions hurt, $pain\n";
return $this->$func($painThreshold - 1);
}
}
class Mouth
{
public $speech = '';
public function __toString()
{
return $this->speech;
}
}
$head = new Head();
$head->recursive();
echo $head->mouth;