9951 explained code solutions for 126 technologies


php-fakerHow do I create a custom provider for PHP Faker?


  1. To create a custom provider for PHP Faker, first you need to create a class that extends Faker\Provider\Base class. This class should contain all the methods you need to generate the data you need.

  2. For example, to create a provider that generates a random color:

<?php

namespace Faker\Provider;

class Color extends Base
{
    public function randomColor()
    {
        return sprintf('#%06X', mt_rand(0, 0xFFFFFF));
    }
}
  1. Then you need to register the provider. This can be done by adding a call to addProvider() method of Faker\Generator class:
<?php

$faker = Faker\Factory::create();
$faker->addProvider(new Faker\Provider\Color($faker));
  1. Now you can use the randomColor() method in your code:
<?php

echo $faker->randomColor();
// Output: #AFCF6B
  1. You can find more information about creating custom providers in the official documentation.

  2. You can also find some useful examples of custom providers in the official repository.

  3. Finally, you can also find some ready-to-use custom providers on Packagist.

Edit this code on GitHub