JPGrefaldo
8 months ago

Automatic Injection on Anonymous Functions?

Posted 8 months ago by JPGrefaldo

I was defining a faker factory when I stumbled upon this question?

<?php

use Faker\Generator as Faker; //<-- tried tinkering but won't work without init from e.i. Faker::create()

//I've wonder how the Faker\Generator  inside the closure is able to do this: $faker->sentence?
$factory->define(App\Project::class, function (Faker $faker) {//does this mean Faker is auto resolve, How?
    return [
        'title' => $faker->sentence,
        'description' => $faker->paragraph
    ];
});

I've tried using just the Faker\Generator on tinker/psySH but it doesn't work.

>>> use Faker\Generator as Faker;
>>> $faker = new Faker; //error should be new Faker::create()
>>>$faker->sentence //error, how come it work on the closure like above??

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