I have a class in PHP like this:
class RandomNumberStorer{
var $integers = [];
public function store_number($int){
array_push($this->integers, $int);
}
public function run(){
generate_number('store_number');
}
}
...elsewhere I have a function that takes a function as a parameter, say:
function generate_number($thingtoDo){
$thingToDo(rand());
}
So I initialise a RandomNumberStorer
and run it:
$rns = new RandomNumberStorer();
$rns->run();
And I get an error stating that there has been a 'Call to undefined function store_number'. Now, I understand that that with store_number
's being within the RandomNumberStorer
class, it is a more a method but is there any way I can pass a class method into the generate_number
function?
I have tried moving the store_number
function out of the class, but then I then, of course, I get an error relating to the reference to $this
out of the context of a class/ instance.
I would like to avoid passing the instance of RandomNumberStorer
to the external generate_number
function since I use this function elsewhere.
Can this even be done? I was envisaging something like:
generate_number('$this->store_number')