On the web name generator that is dating. Faker is a PHP collection that produces fake information for you.

It, or anonymize data taken from a production service, Faker is for you whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test.

Faker is heavily prompted by Perl’s information::Faker, and also by ruby’s Faker.

Faker calls for PHP >= 5.3.3.

dining Table of articles

  • Installation
  • Basic Use
  • Formatters
    • Base
    • Lorem Ipsum Text
    • Individual
    • Target
    • Contact Number
    • Business
    • Genuine Text
    • Time and date
    • Internet
    • Consumer Agent
    • Re Payment
    • Color
    • File
    • Image
    • Uuid
    • Barcode
    • Miscellaneous
    • Biased
    • Html Lorem
  • Modifiers
  • Localization
  • Populating Entities Using an ORM or an ODM
  • Seeding the Generator
  • Faker Internals: Understanding Providers
  • Real World Use
  • Language certain formatters
  • Third-Party Libraries Extending/Based On Faker
  • Permit

Faker supports both PSR-0 as PSR-4 autoloaders.

You can even load Fakers shipped PSR-0 autoloader

instead, you should use any another PSR-4 compliant autoloader

Generate data that are fake

Utilize :create( that is fakerFactory to produce and initialize a faker generator, that may produce information by accessing properties called following the sort of information you prefer.

Regardless if this instance shows a house access, each call to $ yields that are faker->name different (random) result. Simply because Faker makes use of __get() secret, and forwards FakerGenerator->$property telephone calls to FakerGenerator->format($home) .

Suggestion: For a generation that is quick of information, you’ll be able to utilize Faker being a demand line device compliment of faker-cli.

All the generator properties (like title , target , and lorem ) are known as «formatters». A faker generator has its own of these, packed in «providers». Let me reveal a listing of the bundled formatters into the standard locale.

Practices accepting a $timezone argument default to date_default_timezone_get() . It is possible to pass a customized timezone sequence to each technique, or determine a customized timezone for many right time techniques at the same time making use of.

Faker provides three unique providers, unique() , optional() , and valid() , become called before any provider.

If you’d like to make use of a modifier having a value perhaps maybe maybe perhaps not produced by Faker, make use of the passthrough() technique. passthrough() merely comes back whatever value it had been offered.

FakerFactory usually takes a locale as a disagreement, to come back localized information. If no localized provider is discovered, the factory fallbacks towards the standard locale (en_US).

You should check available Faker locales when you look at the supply rule, underneath the company directory. The localization of Faker can be a process that is ongoing which is why we are in need of your assistance. Never think twice to produce localized providers to your very own locale and submit a PR!

Populating Entities Using an ORM or an ODM

Faker provides adapters for Object-Relational and mappers that are object-Documentpresently, Propel, Doctrine2, CakePHP, Spot2, Mandango and Eloquent are supported). These adapters relieve the people of databases through the Entity classes provided by an ORM collection ( or the populace of document shops utilizing Document classes given by an ODM collection).

To populate entities, create a populator that is new (using a generator instance as parameter), then list the course and number of all of the entities that needs to be created. To introduce the real data population, call the execute() technique.

Observe that a few of the populators could need parameters that are additional. As instance the doctrine populator has a choice to specify its batchSize as to how frequently it shall flush the UnitOfWork to your database.

Listed here is an illustration showing how exactly to populate 5 Author and 10 Book things:

The populator makes use of column and name kind guessers to populate each line with appropriate information. As an example, Faker populates a column known as first_name making use of the name that is first, and a line with a TIMESTAMP type with the dateTime formatter. The entities that are resulting consequently coherent. If Faker misinterprets a line title, you are able to still specify a customized closing to be utilized for populating a column that is particular making use of the 3rd argument to addEntity() :

In this instance, Faker will imagine a formatter for several columns except ISBN , which is why the provided function that is anonymous be applied.

Suggestion: To ignore some columns, specify null when it comes to line names within the 3rd argument of addEntity() . Normally, this is required for columns added with a behavior:

Needless to say, Faker will not populate autoincremented keys that are primary. In addition, FakerORMPropelPopulator::execute() comes back the range of inserted PKs, indexed by course:

Note: Due to your undeniable fact that Faker returns most of the main tips placed, the memory usage is certainly going up drastically once you do batch inserts due towards the list that is big of.

In the last example, the Book and Author models share a relationship. Since Author entities are populated first, Faker is sensible adequate to connect the populated Book entities to 1 of this populated Author entities.

Finally, should you want to perform a function that is arbitrary an entity before insertion, make use of the 4th argument associated with addEntity() technique:

Seeding the Generator

You might want to get constantly exactly the same generated information — for example whenever Faker that is using for screening purposes. The generator provides a seed() technique, which seeds the number generator that is random. Calling the exact same script twice with the exact same seed creates equivalent outcomes.

Suggestion: DateTime formatters will not replicate equivalent fake information if you do not fix the $ value that is max

Suggestion: Formatters won’t replicate exactly the same fake information by using the rand() php function. Utilize $faker or mt_rand() rather:

Faker Internals: Understanding Providers

A FakerGenerator alone can’t do much generation. It requires FakerProvider things to delegate the information generation for them. FakerFactory::create() really creates a FakerGenerator bundled because of the standard providers. Some tips about what happens underneath the bonnet:

Once you you will need to access a residential property from the $faker item, the generator actively seeks a technique because of the name that is same all of the providers mounted on it. A call to FakerProviderPerson::name() for instance, calling $faker->name triggers . And since Faker begins because of the provider that is last it is simple to bypass current formatters: simply put in a provider containing practices called following the formatters you intend to bypass.

Meaning that one can effortlessly include your providers that are own a FakerGenerator instance. A provider is generally a classfakerproviderbase that is extending . This moms and dad course enables you to utilize techniques lexify( that is like or randomNumber() ; it offers you usage of formatters of other providers, through the protected $generator property. The formatters that are new the general public ways of the provider class.

Here’s an illustration provider for populating Book information:

To join up this provider, simply include a fresh instance of FakerProviderBook to a current generator:

You will make use of the two formatters that are new just about any Faker formatter:

Suggestion: A provider may also be a Plain Old PHP Object. If so, all of the general public types of the provider become open to the generator.

Real World Use

The script that is following a legitimate XML document:

Operating this script creates a document looking like: