<?php
// Eine Beispiel-Callback-Funktion
function my_callback_function() {
echo 'hello world!', PHP_EOL;
}
// Eine Beispiel-Callback-Methode
class MyClass {
static function myCallbackMethod() {
echo 'Hello World!', PHP_EOL;
}
}
// Typ 1: Einfaches Callback
call_user_func('my_callback_function');
// Typ 2: Statischer Methodenaufruf
call_user_func(array('MyClass', 'myCallbackMethod'));
// Typ 3: Aufruf einer Objektmethode
$obj = new MyClass();
call_user_func(array($obj, 'myCallbackMethod'));
// Typ 4: Statischer Methodenaufruf
call_user_func('MyClass::myCallbackMethod');
// Typ 5: Relativer statischer Methodenaufruf
class A {
public static function who() {
echo 'A', PHP_EOL;
}
}
class B extends A {
public static function who() {
echo 'B', PHP_EOL;
}
}
call_user_func(array('B', 'parent::who')); // A, seit PHP 8.2.0 veraltet
// Typ 6: Objekte die __invoke implementieren können als Callable verwendet werden
class C {
public function __invoke($name) {
echo 'Hello ', $name, PHP_EOL;
}
}
$c = new C();
call_user_func($c, 'PHP!');
?>