GeneralFunctions\sideEffect()

higher-order returns Closure
((T) → any) → (T → T)
At a glance — "Tap" into a pipeline. Drop a sideEffect anywhere inside compose or pipe to observe or log the value without altering it.

Creates a Closure that runs an interceptor callable for its side effect and returns the original input unchanged. Ideal for logging or debugging inside a pipeline without breaking it.

/**
  * @param mixed $interceptor Any callable — string function name, Closure, array callable, or invokable object.
  * @return Closure
  */
GeneralFunctions\sideEffect($interceptor): Closure

Returned Closure

When GeneralFunctions\sideEffect() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param mixed $value
  * @return mixed The same $value, untouched.
  */
$function ($value)

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

$logger = GeneralFunctions\sideEffect(fn($v) => error_log("value: $v"));

$result = GeneralFunctions\pipe(
  '  Foo  ',
  'trim',
  $logger,          // logs "value: Foo", then passes it on
  'strtolower'
);

echo $result; // 'foo'

Details

General Functions

Releated Combinators Functions