Skip to content

Latest commit

 

History

History
65 lines (47 loc) · 1.97 KB

format.md

File metadata and controls

65 lines (47 loc) · 1.97 KB

Callback format

Callback format which provides by the library is backward compatible with the standard format.

Variants of the standard format:

  • string "functionName"
  • array [$object, "methodName"]
  • array ["ClassName", "staticMethodName"]
  • string "ClassName::staticMethodName"
  • an object with a method __invoke
  • Closure

All of these variants are supported in the library.

The numeric array

Bound arguments are specified in the element with index 2 as a numeric array.

  • [$object, "methodName", $args]: calls $object->methodName() with arguments from $args.
  • ["ClassName", "staticMethodName", $args]
  • ["ClassName::staticMethodName", null, $args] - similar to the previous
  • [null, "funcName", $args] - global function (without class)
  • [$object, null, $args] - call the object as function (__invoke())
  • [null, $closure, $args]

Example:

$callback = [$object, 'methodName', [1, 2]];

Callback::call($callback, [3, 4]); // $object->methodName(1, 2, 3, 4)

The optional flag for bind context are specified in the element with index 3.

  • [$object, "methodName", $args, true]
  • [$object, "methodName", null, true] - bind context without arguments
  • ...
$callback = [$object, 'privateMethodName', [1, 2], true];

Callback::call($callback, [3, 4]); // $object->privateMethodName(1, 2, 3, 4)

Full format

The associative array. All fields are optional.

  • function - a global function name or a closure.
  • object - an object. Needs method (or __invoke by default)
  • class - a class name. Needs method for static method name.
  • method - a method name. Needs object or class.
  • args - a list of arguments for binding (numeric array).
  • bind - a bind flag (boolean, FALSE by default).
$callback = [
    'class' => 'MyClass',
    'method' => 'privateStaticMethodName',
    'bind' => true,
];