In PHP, I would like to refer to a member function.
For general functions I can assign the function to a variable, like so:
php > function h1() {echo "h1";}; h1();
h1
php > function h2() {echo "h2";}; h2();
h2
php > $hh = h1; $hh(); // ok: assign to a variable and call
h1
php > $hh = h2; $hh(); // ok: assign to a variable and call
h2
This is useful if you want to pass a function as an argument to another function.
But what about member functions?
class C {
function k(){ echo "k";}
function j(){ $a = $this->k; $a();} // assign k() to a variable
function m(){ $a = "k"; $this->$a();} // assign the name "k" to a variable
};
This works:
php > $c = new C();
php > $c->k();
k
> $c->m(); // variable-function approach
k
But this does not (tested in CLI)
php > $c->j();
Warning: Uncaught Error
Is my syntax wrong, or is the variable-function approach the only way?