Page 1 of 1

Patterns For PHP

Posted: Tue Jul 04, 2006 10:17 am
by Maugrim_The_Reaper
Been aiming to put this up during the Summer, but then someone suggested a PHP Book project and I decided just to get it done.

Since it will likely be used for some later book contributions, I'm just throwing it out there for discussion and possible contributions. It's a wiki, not unlike http://phppatterns.com which will be a repository of patterns specifically implemented for PHP.

http://patterns.quantum-star.com (dedicated domain to come)

Have added the Singleton and Registry patterns as a small start. I am converting a prior blog post on the Factory and Abstract Factory patterns. Public submissions are welcome as detailed on the front page with any texts made available to the PHP Book project with any other contributions on design patterns which are sure to pour in over time.

Posted: Tue Jul 04, 2006 11:16 am
by Christopher
Good idea. Phppatterns.com has been out of date for a very long time -- and honestly it wasn't that great to start with.

I really like that you have shown two example of implementing the pattern for a real use case -- instead of just creating eponymous classes

PS - I checked out the Singleton. As I recall the PHP4 version is better if you assign as $thisInstance[0] = new Settings(); because of something about static vars in PHP4 -- but I can't rememeber what.

Posted: Tue Jul 04, 2006 11:19 am
by Jenk
I was going to post that $this causes an error/notice when used in a static function, but alas, too slow :)

Posted: Tue Jul 04, 2006 11:53 am
by Maugrim_The_Reaper
If either of you want write access just give me a shout. Any contributions would be welcome. It's closed just for the moment until I fix up a domain for it and then public access will be enabled. The texts are still initial drafts - there'll likely be other small errors until I get around to reviewing them all this evening.

I'll look into that static var issue for PHP - is this specific to any version range?

Factory Pattern added to the mix...

Posted: Tue Jul 04, 2006 12:19 pm
by Christopher
I'd like to help out.

I also think that the site should somehow have dual indexes -- one by pattern and one by use case (which might employ several patterns). So if you looked up DB Connection class you might find examples under Factory and SIngleton. That would be useful and interesting.

Posted: Tue Jul 04, 2006 4:13 pm
by Maugrim_The_Reaper
I'll setup an account for you and send you a PM. This is intended as a public wiki, so if you have an idea - go with it.

I also intend to include at some point a section on the principles behind design patterns, since its highly relevant material and explains a lot about how and why patterns exist, i.e. coding to an interface, polymorphism, decoupling, etc. For the moment the pattern section is seeing attention mainly - but feel free to expand.

Also the current texts are initial drafts - they are intended to be elaborated upon over time hopefully in a format which offers a simplified overview/summary followed by more in depth detail. At present I'm just following a standard section order to get the initial text setup. Unit Tests for the patterns are also intended.

I natter on ;).

Posted: Tue Jul 04, 2006 7:01 pm
by sweatje
arborint wrote: PS - I checked out the Singleton. As I recall the PHP4 version is better if you assign as $thisInstance[0] = new Settings(); because of something about static vars in PHP4 -- but I can't rememeber what.
actually $thisInstance[0] =& new Settings;

Due to the fact that a static can't hold a reference, but an element of a static array can.

http://www.php.net/manual/en/language.v ... references


Oh, and thank goodness I don't have to mess around with all those ampersands any more in PHP5 (unless I really mean if for pass by ref in functions). :D

Posted: Tue Jul 04, 2006 7:29 pm
by Todd_Z
Wow... i just bought sweatje's new book - I had no idea he was a part of this forum.

Guess now I can't bash it!

Just kidding, its a great read, well done.

Posted: Wed Jul 05, 2006 3:33 am
by Maugrim_The_Reaper
Due to the fact that a static can't hold a reference, but an element of a static array can.
This is an interesting point, because I remember pulling my hair out over this a long while back. I use few singletons but since that exercise I usually drop the ampersand reference character (hence the example's form). I amended the PHP 4 example and added a short explanation and link to the above part of the manual.

Posted: Wed Jul 05, 2006 11:50 am
by feyd
I was thinking about doing something similar in the last few weeks. The basic idea I had was to walk through the PoEAA and GoF books (to start) and build most, if not all, of those patterns into PHP variants. Although I'm interested to help, I don't know about the time I'd need to write and document these patterns.

Posted: Wed Jul 05, 2006 3:54 pm
by Maugrim_The_Reaper
It will be a public wiki on a dedicated domain soon, so whenever the mood strikes...:)

Posted: Thu Jul 06, 2006 8:36 am
by Maugrim_The_Reaper
A MediaWiki version of the repository is now being spruced up:

http://www.quantum-star.com/mediawiki/

Posted: Thu Jul 06, 2006 11:13 am
by Maugrim_The_Reaper
And now finished (I think). Back at http://patterns.quantum-star.com .

Posted: Thu Jul 06, 2006 1:18 pm
by alvinphp
very nice stuff reaper. patterns really help make coding easier and more structured, especially in PHP where you are allowed to get away with a little bit more. :D

Posted: Thu Jul 06, 2006 3:12 pm
by Nathaniel
I'm looking forward to what this looks like it will become. :)