Executing calling class' method
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开发者_StackOverflow中文版 calling class. Right now, it's looking for a global function
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();
}
}
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 ^^.)
精彩评论