Faker – The ultimate Lorem Ipsum for PHP

From time to time we all have to work with data which doesn’t exist. The best example is web development. Front-end developers create HTML and CSS before any web content is created. The common practice is to use Lorem Impsum which proved to be effective strategy. It’s not much different with back-end programming. We need dummy data at least as often as front-end devs. The most popular cases are:

– emulating user input
– emulating external service
– filling database (for development, presentation or performance test)
– mocking object for Unit Test

It’s always better to work with real or at least close to real content. First of all there is a psychological aspect – it looks better. When your database is filled with identical rows even a completed code will look like work in progress. Quality data gives more confidence. It’s good to know will code work with something more then a fix string. Finally there is a practical reason. A good example would be performance test. Without good realistic data you might measure values which will never occur in the real live.

Generating good dummy data manually is a cumbersome process. It’s very tempting to take shortcuts. Fortunately there is no need to do it “by hand”.

Faker is a PHP library that generates fake data for you. Whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service, Faker is for you.

To install Faker download package from GitHub or use the Composer. I prefer to use Composer. Give it a try If you don’t have it.

$ curl -s http://getcomposer.org/installer | php

Create (or edit) composer.json

$ vim composer.json

and add “fzaninotto/faker”: “v1.1.0”.

    "require": {
        "fzaninotto/faker": "1.1.0"

Now you are ready to install Faker.

$ php composer.phar install

Once you are all setup with the library you can try this example.


require_once "vendor/autoload.php";

$faker = FakerFactory::create();

for( $x=0 ; $x<5 ; $x++ ) {
 printf( "%s  from %sn", 
  $faker->city );

It should return something like this:

$ php test.php
Francisco Boehm  from Jasminmouth
Richard Hettinger  from North Shad
Ethyl Hudson  from Metzmouth
Ansley Reilly  from Schaeferburgh
Willa Hayes MD  from East Nicole

Faker can provide all sort of random data like: Addresses, Telephone Numbers, Companies, Domains, Emails, Ips, DataTime and of course Lorem Ipsum. To see full list of all available features have a look into the manual.

If that is not enough Faker has one more very cool feature. It can work with popular ORMs like Propel, Doctrine2 and Mandango.

addEntity('Author', 5);
$populator->addEntity('Book', 10);
$insertedPKs = $populator->execute();

The example will insert 5 Authors and 10 Books into a database. Faker tries to figure out on it’s own what type of data is required for each attribute. It’s obviously unable to guess everything but still, it does a great job.

Faker is great and very usefully library. It provides impressive features wrapped in a clear API with good documentation. When you need to generate some dummy data Faker is the way to go.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s