diff --git a/Adapter/Adapter.php b/Adapter/Adapter.php index 0231bb8..3ecb4ed 100644 --- a/Adapter/Adapter.php +++ b/Adapter/Adapter.php @@ -12,7 +12,7 @@ namespace DesignPatterns; * Examples: * - different databases have the same interface to communicate although the underlying systems act differently * - * this is a VERY basic example which won't work at all + * this is a VERY basic example which won't work at all! */ interface DatabaseAdapter { diff --git a/Facade/Facade.php b/Facade/Facade.php index 46be4d4..60bfd7e 100644 --- a/Facade/Facade.php +++ b/Facade/Facade.php @@ -10,6 +10,8 @@ namespace DesignPatterns; * * Examples: * - Database Abstraction Layers + * - Doctrine2: EntityManager is the facade that one sees from the outside, but in there is much more going on, Unit of + * Work, etc. * */ class Facade diff --git a/FluentInterface/FluentInterface.php b/FluentInterface/FluentInterface.php new file mode 100644 index 0000000..8d2fff2 --- /dev/null +++ b/FluentInterface/FluentInterface.php @@ -0,0 +1,59 @@ +_fields = $fields; + return $this; + } + + /** + * + * @param string $table + * @param string $alias + * @return SQL + */ + public function from($table, $alias) + { + $this->_from[] = $table . ' AS ' . $alias; + return $this; + } + + /** + * @param string $condition + * @return SQL + */ + public function where($condition) + { + $this->_where[] = $condition; + return $this; + } +} + +$instance = new SQL(); +$instance->select(array('foo', 'bar')) + ->from('foobar', 'f') + ->where('f.bar = ?'); \ No newline at end of file diff --git a/Multiton/Multiton.php b/Multiton/Multiton.php index bd5adb8..4327a0e 100644 --- a/Multiton/Multiton.php +++ b/Multiton/Multiton.php @@ -46,6 +46,7 @@ class Multiton /** * gets the instance with the given name, e.g. Multiton::INSTANCE_1 + * uses lazy initialization * * @param string $instanceName * @return Multiton diff --git a/Prototype/Prototype.php b/Prototype/Prototype.php index 913be81..00036b2 100644 --- a/Prototype/Prototype.php +++ b/Prototype/Prototype.php @@ -6,7 +6,7 @@ namespace DesignPatterns; * Prototype pattern * * Purpose: - * to avoid the the cost of creating objects the standard way (new Foo()) and instead create a prototype and clone it + * to avoid the cost of creating objects the standard way (new Foo()) and instead create a prototype and clone it * * Examples: * - Large amounts of data (e.g. create 1,000,000 rows in a database at once via a ORM) diff --git a/README.markdown b/README.markdown new file mode 100644 index 0000000..3faef03 --- /dev/null +++ b/README.markdown @@ -0,0 +1,8 @@ +# design patterns in PHP # + +This is a collection of known design patterns and some sample code how to implement them in PHP. Every pattern has a +small list of examples (most of them from Zend Framework or Doctrine2 as I'm most familiar with this software). + +I think the problem with patterns is that often people do know them but don't know when to apply which. + +*Please feel free to fork and add your own examples!* diff --git a/Singleton/Singleton.php b/Singleton/Singleton.php index 16d4ca0..4da63d2 100644 --- a/Singleton/Singleton.php +++ b/Singleton/Singleton.php @@ -6,11 +6,11 @@ namespace DesignPatterns; * Singleton pattern * * Purpose: - * - to have only one instance of this object in the application, that handles all calls + * to have only one instance of this object in the application, that will handle all calls * * Examples: * - DB Connector - * - Logger (May also be a Multiton if there are many Logfiles for several purposes) + * - Logger (may also be a Multiton if there are many log files for several purposes) * - Lock file for the application (there is only one in the filesystem ...) * */