I was trying to write js-like php, using closures. However, I don't understand why I can't assign a closure to a stdClass property.
The code explains itself:
$sum = function ($a, $b) {return $a + $b;};
echo $sum(11, 11);
// prints 22
$arr = [];
$arr['sum'] = function ($a, $b) {return $a + $b;};
echo $arr['sum'](22, 22);
// prints 44
$cl = new stdClass;
$cl->sum = function ($a, $b) {return $a + $b;};
echo $cl->sum(33, 33);
// Fatal error: Uncaught Error: Call to undefined method stdClass::sum()
# although I can't think of any use cases for this
class Custom {
public $sum = NULL;
function __construc() {
$this->sum = function ($a, $b) {return $a + $b;};
}
}
$custom = new Custom;
echo $custom->sum(44, 44);
// Fatal error: Uncaught Error: Call to undefined method Custom::sum()