PHP code example of adhocore / with

1. Go to this page and download the library: Download adhocore/with library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

adhocore / with example snippets


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

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

// 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']);

// 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();