I've just recently began messing around with anonymous functions, and I decided to try and put it to actual use in a larger project. After hitting a road block, I tried setting up a small practice script, but the same issue persists. I don't quite understand what's going on, but here's some code,
$a = function($in) {
echo $in;
};
$a('b4ng');
The above works just fine, as does the following,
class Foo {
public $cmd;
public function __construct() {
$this->cmd = new stdClass();
$this->cmd->a = 'b4ng';
}
}
$bar = new Foo();
echo $bar->cmd->a;
That being made clear, the following does not work,
class Foo {
public $cmd;
public function __construct() {
$this->cmd = new stdClass();
$this->cmd->message = function($in) {
echo $in;
};
}
}
$bar = new Foo();
$bar->cmd->message('b4ng');
When attempting to use the snippet above, I'm hit with the following error,
Fatal error: Call to undefined method stdClass::message()
I understand what the error is telling me, I just don't understand why; 'message' obviously isn't a native function/method of stdClass, but it was declared in 'cmd'.