Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

RubySource.com:
Confessions of a Converted PHP Developer: Animal Abuse
Jun 27, 2011 @ 18:40:02

In another entry in his "Confessions of a Converted PHP Developer" series Mal Curtis about a typical class development path PHP developers take and how it can be a limiting, linear progression from one step to another.

In converting to Ruby I realized how strict PHP is in its class hierarchy. This surprised me as I'd always viewed PHP as a very loose language, mainly due to its loose typing and lack of formal code structure guidelines. [...] I find most PHP developers learn starting by using inline PHP as a low learning curve entry point into dynamic web languages and then move on to fuller, more complex, applications.

He includes a few code samples comparing how Ruby, while still allowing the same kind of structure, also lets you redefine classes on the fly. His example shows redefining a method in a Ruby class using a method commonly called "monkey patching". It's good that he points out a downfall of the approach too:

I must note that Monkey Patching is often a quick fix solution that can create headaches for future developers (or for yourself, if your memory is like mine) for several reasons. [...] Use at your own risk!

There's several different articles all over the web talking about the "monkey patching" approach and if it's possible/useful in PHP.

tagged: monkeypatching ruby compare class objectoriented rubysource

Link:

Gonzalo Ayuso's Blog:
Using Monkey Patching to store files in CouchDb using the standard filesystem
Sep 02, 2010 @ 19:10:12

Gonzalo Ayuso takes his "CouchDb as a filesystem" approach one step further (see the previous post about it here) with this new post talking about monkey patching to store files into the CouchDb server using the normal PHP file handling functions.

Since PHP5.3 a new design pattern is available for us: Monkey Patching. With this pattern we can override PHP's core functions with a home-made functions in a different namespace (another example here). That's means if I have fopen function in the above example, PHP uses the filesystem function "fopen" but if we set a namespace in our example, PHP will search first the function within the current namespace.

By defining the new interface inside of a namespace (with functions to override the default PHP file handlers) you can have the rest of the code call the same functions (fopen, fread, etc) but they'll do different things. In this case it handles them as push and pull to the CouchDb instead of the normal filesystem. You can grab the source for this example here.

tagged: monkeypatching namespace filesystem couchdb tutorial

Link:


Trending Topics: