Skip to content

Instantly share code, notes, and snippets.

@pscheit
Created November 20, 2017 21:26
Show Gist options
  • Select an option

  • Save pscheit/07c5e33822d251551c7571fa81e87b06 to your computer and use it in GitHub Desktop.

Select an option

Save pscheit/07c5e33822d251551c7571fa81e87b06 to your computer and use it in GitHub Desktop.

Revisions

  1. pscheit created this gist Nov 20, 2017.
    31 changes: 31 additions & 0 deletions closure.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    <?php

    class Closures
    {

    public static function create($methodName)
    {
    return function($thing) use ($methodName) {
    return $thing->{$methodName}();
    };
    }
    }

    class Thing
    {

    public function __construct($value)
    {
    $this->value = $value;
    }

    public function getSomething()
    {
    return $this->value;
    }
    }


    $things = [new Thing(1), new Thing(2), new Thing (3)];

    print_r(array_map(Closures::create('getSomething'), $things));