1
class Car { $gas= new Gas(); $gas->fill( 'filledHandler' ); function filledHandler() { echo 'Gas has been filled!'; } } class Gas { function fill( $function ) { // do something here $function(); } } 

I need to call $function of calling class. Right now, it's looking for a global function

0

2 Answers 2

4

You have to pass the calling instance.

class Car { function fillGas() { $gas = new Gas(); $gas->fill($this, 'filledHandler'); } function filledHandler() { echo 'Gas has been filled!'; } } class Gas { function fill($obj, $function) { // If you need the class name, use get_class($obj) $obj->$function(); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Grrrr.... I wasted half an hour on this thing, looking for ways to find which class is calling the method. I was looking in all the wrong places. Thank you BC!
0
class Car { function __construct() { $gas= new Gas(); $gas->fill($this, 'filledHandler' ); } function filledHandler() { echo 'Gas has been filled!'; } } class Gas { function fill($object, $function ) { $object->$function(); } } 

(Ask more question (like what exactly do you want to happen anyway) and get more answer text ^^.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.