Patterns For PHP

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Patterns For PHP

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I was going to post that $this causes an error/notice when used in a static function, but alas, too slow :)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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 ;).
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

It will be a public wiki on a dedicated domain soon, so whenever the mood strikes...:)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

A MediaWiki version of the repository is now being spruced up:

http://www.quantum-star.com/mediawiki/
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

And now finished (I think). Back at http://patterns.quantum-star.com .
alvinphp
Forum Contributor
Posts: 380
Joined: Wed Sep 21, 2005 11:47 am

Post 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
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

I'm looking forward to what this looks like it will become. :)
Post Reply