Login Register






Professional PHP Design Patterns filter_list
Author
Message
Professional PHP Design Patterns #1

[Image: 8bcbe135179a4a92ece3c77d03a9a80f.jpeg]

Aaron Saray, "Professional PHP Design Patterns"
Wrox | ISBN: 0470496703 | 2009 | 288 Pages | PDF | 5 MB
This guide helps PHP developers take advantage of the stability and features of design patterns
Design patterns are the cornerstones of building solid, stable, flexible, and feature-rich Web applications. This guide enables PHP developers to take advantage of everything they offer.
If you are unfamiliar with design patterns, this book explains what you need to know. Both novice and veteran PHP developers will benefit from the alphabetical list of design patterns and code examples showing how to implement each pattern in PHP. Step-by-step instructions for a sample contact management system will help you understand real-world applications for the information.


download

http://filemac.com/bltjtuz91oi6/GFX-Prof...tterns.pdf


Reply

RE: Professional PHP Design Patterns #2
Oh wow, I just skimmed through that real quick. I can definitely write a better book than that. In reality that isn't a PHP oriented book. It is a basic OOP book. Half of the design patterns in there they talk about as if it is a class to be designed when most of them are interfaces already provided for you to implement in your classes.

It's a novice level book for getting started in OOP. I don't suggest it to anyone beyond that because it may cause confusion if you already have an understanding of inheritance and interfaces.

Also, the Singleton design pattern. IMO it is one of the most important design patterns in any type of application that you would like to have plugins interact with them. They did it all wrong. With their implementation there is no way to have a Singleton Superclass.

In their code:
Spoiler:
PHP Code:
class InventoryConnection { protected static $_instance = NULL; protected $_handle = NULL; public static function getInstance() { if (!self::$_instance instanceof self) { self::$_instance = new self; } return self::$_instance; } protected function __construct() 105 Part II: Reference Material { $this->_handle = mysql_connect(‘localhost’, ‘user’, ‘pass’); mysql_select_db(‘CD’, $this->_handle); } public function updateQuantity($band, $title, $number) { $query = “update CDS set amount=amount+” . intval($number); $query .= “ where band=’” . mysql_real_escape_string($band) . “’”; $query .= “ and title=’” . mysql_real_escape_string($title) . “’”; mysql_query($query, $this->_handle); } }


They have a design pattern with absolutely no setup for code reuse. That is one of the main concepts of OOP, don't duplicate code... EVER... If you find yourself looking at another class to see how you should write something, or to copy and paste, that code should be refactored elsewhere so all interested parties can take advantage of it.

I would have done this:
Spoiler:
PHP Code:
class Super_Singleton { protected static $_instances; /** * Create a new instance if one does not exist. * Store our own instance and return the reference. * @return Super_Singleton The instance of $this. */ public static function getInstance() { $me = get_called_class(); if (!isset(self::$_instances[$me])) { $args = func_get_args(); $ref = new ReflectionClass($me); self::$_instances[$me] = $ref->newInstanceArgs($args); } return self::$_instances[$me]; } /** * We can create a new instance if one does not exist. * @throws Exception Attempted instantiation of an already instantiated Singleton. */ public function __construct() { $me = get_class($this); if (isset(self::$_instances[$me])) { throw new Exception('Attempted instantiation of an already instantiated Singleton.'); } else { self::$_instances[$me] = $this; } } /** * Disallow cloning. */ public function __clone() { trigger_error("Cloning is not allowed.", E_USER_WARNING); } /** * Disallow serializing. */ public function __wakeup() { trigger_error("Unserializing is not allowed.", E_USER_ERROR); } } class Inventory_Connection extends Super_Singleton { protected $_handle = NULL; public function __construct() { parent::__construct(); $this->_handle = mysql_connect(‘localhost’, ‘user’, ‘pass’); mysql_select_db(‘CD’, $this->_handle); } public function updateQuantity($band, $title, $number) { $query = “update CDS set amount=amount+” . intval($number); $query .= “ where band=’” . mysql_real_escape_string($band) . “’”; $query .= “ and title=’” . mysql_real_escape_string($title) . “’”; mysql_query($query, $this->_handle); } }


In my method, you take out the ugly design pattern code from your class and can really focus on its intention. Also, this Singleton Superclass can be used by any other Singleton you may have. Another change that I had made was the addition of the clone and wakeup magic methods to more properly follow the Singleton design pattern.

Reply

RE: Professional PHP Design Patterns #3
Theirs is a more "true" singleton in the design pattern sense. Yours would be classified as a Singleton Registry. Also if you're implementing the Singleton pattern why not just make the constructor private and force the use of getInstance method? I'm not criticizing your way of doing it, because it is more reusable and better than a traditional singleton.

Reply

RE: Professional PHP Design Patterns #4
thanks for sharing this ebook

Reply

RE: Professional PHP Design Patterns #5
thanks for sharing this ebook
PM Me For Web Designing in Cheap Rates

Reply

RE: Professional PHP Design Patterns #6
Quote: slbmeh

Oh wow, I just skimmed through that real quick. I can definitely write a better book than that. In reality that isn't a PHP oriented book. It is a basic OOP book. Half of the design patterns in there they talk about as if it is a class to be designed when most of them are interfaces already provided for you to implement in your classes.

It's a novice level book for getting started in OOP. I don't suggest it to anyone beyond that because it may cause confusion if you already have an understanding of inheritance and interfaces.

Also, the Singleton design pattern. IMO it is one of the most important design patterns in any type of application that you would like to have plugins interact with them. They did it all wrong. With their implementation there is no way to have a Singleton Superclass.

In their code:
Spoiler Click to View
They have a design pattern with absolutely no setup for code reuse. That is one of the main concepts of OOP, don't duplicate code... EVER... If you find yourself looking at another class to see how you should write something, or to copy and paste, that code should be refactored elsewhere so all interested parties can take advantage of it.

I would have done this:
Spoiler Click to View
In my method, you take out the ugly design pattern code from your class and can really focus on its intention. Also, this Singleton Superclass can be used by any other Singleton you may have. Another change that I had made was the addition of the clone and wakeup magic methods to more properly follow the Singleton design pattern.


Cant wait for your e-book then
<?php echo "Very inactive at this current moment in time"; ?>
[Image: Z8KDe.png]

Reply

RE: Professional PHP Design Patterns #7
(11-20-2011, 09:43 AM)puma Wrote: Theirs is a more "true" singleton in the design pattern sense. Yours would be classified as a Singleton Registry. Also if you're implementing the Singleton pattern why not just make the constructor private and force the use of getInstance method? I'm not criticizing your way of doing it, because it is more reusable and better than a traditional singleton.

The premise of their book is generally to use these design patterns and extend them for your classes. Their implementation would not be a very extendable singleton. To do a traditional singleton in PHP you need to write the singleton code in every class that you want implemented as singleton. In my method, it's not as traditional, but the intent is to remove repeat code and copy/paste errors.

Reply