Iam working on sample codes in php, I read the variable functions (which is kind of similar to function pointers in C) and started to write below code. Take a look at this
class fruit{
public $run;
}
function ano1(){
// some statements
}
$x = 'ano1'; // variable function
$x(); // works
$f = new fruit;
$f->run = 'ano1'; // lets store function to $run property
$f->run(); // call to undefined method why?
Reason I think
because it needs $this
reference to be passed implicitly to ano1()
which is not a member function? So using $this is not valid inside ano1()
they simply did not implement it yet? But Iam a c++ background which is quite possible in c++:
class fruit{
public : void (*run) ();
};
void ano1(){ cout << "im running" << endl; }
int main(){
fruit f;
f.run = ano1;
f.run(); // works as long as you dont use this
}
Or is there a workaround in php as well? (Without closures and anonymous functions). Thank you