Defining entities to match database tables

A very common practice among PHP developers is to create classes that represent database tables. Such classes are often referred to as entity classes, and form the core of the domain model software design pattern.

How to do it...

  1. First of all, we will establish some common features of a series of entity classes. These might include common properties and common methods. We will put these into a Application\Entity\Base class. All future entity classes will then extend Base.
  2. For the purposes of this illustration, let's assume all entities will have two properties in common: $mapping (discussed later), and $id (with its corresponding getter and setter):
    namespace Application\Entity; class Base { protected $id ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.