JPGrefaldo
8 months ago
115
7
General

About Closures

Posted 8 months ago by JPGrefaldo
// I stumbled upon this on the Collection class

    protected function valueRetriever($value)
    {
        if ($this->useAsCallable($value)) {
            return $value;
        }

        return function ($item) use ($value) {
    //using keyBy method with a callback I wonder how can be the $item variable gets it value?
        dd($item);
            return data_get($item, $value);
        };
    }


// I tried replicating the method but I got no output
    class hello {
        function get($value){
            return function ($item) use ($value) {
        dd($item);
                return $item;
            };
        }
    }

    $hello = new hello;
    $hello->get("hello");

Please sign in or create an account to participate in this conversation.