Skip to content

A simple callable wrapper that allows better management of call parameters

Notifications You must be signed in to change notification settings

wyrfel/php-callback

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Maintainability Test Coverage Latest Stable Version Total Downloads License

php-callback

This is a simple callable helper that allows better control over the call parameters on the final call.

Concepts

As opposed to an immediate function call, that accepts one set of parameters, when passing a callback, there are two occasions at which parameters may be passed - once at the time the callback is created/passed, the other at the time of the call.

We will call the former the instantiation arguments as they are passed at the time of the instantiation of the callback helper - and we'll call the latter the invocation arguments as they are passed at the time of invocation of the callback.

With a regular php callable, parameters can only be passed at the time of the call. An exception is the array_walk built-in that allows the specification of a userdata parameter when passing the callback that is then appended to the call parameters at the time of the call. This helper does the same thing, but with a few more capabilities.

When there are two sets of parameters, but the callable can of course only accept one set of parameters, the question arises how to merge the two sets into one. This helper is configurable to

  • append the invocation arguments to the instantiation arguments $callable(A1, A2, A3, B1, B2)
  • prepend the invocation arguments to the instantiation arguments $callable(B1, B2, A1, A2, A3)
  • ignore the invocation arguments altogether $callable(A1, A2, A3)
  • override the instantiation arguments with the invocation arguments based on position $callable(B1, B2, A3)

(with An begin the instantiation arguments an Bn being the invocation arguments)

Usage

$callback = new Callback([$this, 'methodName'], $withThis, $andThat);

$callback->appendInvocationArguments();
$callback->prependInvocationArguments();
$callback->ignoreInvocationArguments();
$callback->overrideInstantiationArguments();

preg_replace_callback($regexp, $callback, $subject);

About

A simple callable wrapper that allows better management of call parameters

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages