Implementing a form factory

The purpose of a form factory is to generate a usable form object from a single configuration array. The form object should have the ability to retrieve the individual elements it contains so that output can be generated.

How to do it...

  1. First, let's create a class called Application\Form\Factory to contain the factory code. It will have only one property, $elements, with a getter:
    namespace Application\Form;
    class Factory
      protected $elements;
      public function getElements()
        return $this->elements;
      // remaining code
  2. Before we define the primary form generation method, it's important to consider what configuration format we plan to receive, and what exactly the form generation will produce. For this illustration, we ...

Get PHP 7 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.