php callback function to call several gestures

A little summary encountered when moving bricks on the callback function (callback function) of.

  1. General function
$this->callSomeFunction('some_global_php_function');
  1. Object instance
// Only from inside the same class
$this->callSomeFunction([$this, 'myCallback']);

// From either inside or outside the same class
$myObject->callSomeFunction([new MyClass(), 'myCallback']);

Note: The callback function available to other objects do not forget access control (public).

  1. Static class methods
// Only from inside the same class
$this->callSomeFunction([__CLASS__, 'myStaticCallback']);

// From either inside or outside the same class
$myObject->callSomeFunction(['\Namespace\MyClass', 'myStaticCallback']);
$myObject->callSomeFunction(['\Namespace\MyClass::myStaticCallback']); // PHP 5.2.3+
$myObject->callSomeFunction([MyClass::class, 'myStaticCallback']);     // PHP 5.5.0+
  1. Anonymous function
$this->callSomeFunction(function() {
	// do something in anonymous function
});
Published 18 original articles · won praise 1 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_42557486/article/details/101783738