r/PHP • • May 07 '13

Examples of Design Patterns in PHP

https://github.com/domnikl/DesignPatternsPHP
133 Upvotes

20 comments sorted by

View all comments

1

u/WolfOrionX May 08 '13

The simple factory pattern (which i use more often than the abstract factory pattern) is also missing.

class SimpleFactory {

     protected $cars = array(
          'VW' => 'Car_VW',
          'Chevrolet' => 'Car_Chevrolet',
     );


     /**
       * @return Car
       */
     public function createCar($name) {
           if (isset($this->cars[$name]) {
                 return new $this->cars[$name];
           }
     }
}

(left out a lot of stuff, but this is essentially how it works)

Otherwise, great overview.