With provides object like fluent interface for scalars and non objects

v0.0.3 2017-10-26 15:50 UTC


Latest Version Travis Build Scrutinizer CI Codecov branch StyleCI Software License

  • Objectify scalars and non objects
  • Fluent method chaining instead of nested function calls
  • For PHP7


composer require adhocore/with


use function Ahc\with;
// OR
use Ahc\With\With;

$val  = ['a' => 10, 'b' => 12, 'c' => 13];
$with = with($val) // OR (new With($val))
    // _ at the end means the current value is appended to the supplied arguments (default is prepend).
    ->array_map_(function ($v) { return $v + 2; })

// Get the final result
echo $with(); // 41

// Passing value through closures or class methods:
with($value)->via(function ($val) { return $val; });
with($value)->via([new SomeClass, 'method']);


TL;DR: Provides more intuitiveness, comprehensibility and less cognitive overhead than nested function calls.

Did you ever had to pass a scalar/non-object through many layers of functions? Then you might have probably ended up with having to call the first the function which had to be called at the last logically. For example if you want to sum the keys of an array after adding 2 to them?

// Without:
array_sum(array_map(function ($a) { return $a + 2; }, array_keys($array)));

// With:
with($array)->array_keys()->array_map_(function ($a) { return $a + 2; })->array_sum();