views:

177

answers:

7

Hello, I've recently became interested in proper OOP design in web applications. I think I understand most of the principles and design patterns but sometimes I have problem with putting them into practice.

I use MVC and I think I am able to design controllers and views in OOP way. The problem I face is with models. I'm particularly obsessed with dependency injection and inversion of control in general. It works nice in small examples but I have trouble to design complex multi layered models - I'm thinking about various ways to inject dependencies into lower layers etc.

So I decided to look for some projects made by more experienced OOP programmers which I could study. I'm looking for PHP web application, preferably made with MVC architectural pattern. Also I don't mind if it has anemic model (which is usually considered to be antipattern but in heavy data based applications I'm often forced to use anemic models).

Thanks a lot.

EDIT: I'm not looking for a framework but for a complete application. Frameworks usually have not a lot of to do with model architecture.

A: 

The CodeIgniter framework comes to mind as a decent study source

Peace, Dan

Dan
Thanks, but I'm not looking for a framework but for complete application. Frameworks deal with models only shallowly (or not at all).
Tomik
@Tomik How so? That seems kind of a broad-sweeping statement to make about "frameworks" in general. In my experience, CodeIgniter perfectly handles separation of layers while being lightweight and uncluttered.
treeface
You've got to be kidding me. CodeIgniter is the nowhere near excellent OOP design. Just look at the dependency from _everything_ on the CodeIgniter super object.
Magnus Nordlander
:P I said a Decent study source, not the end all sheesh You must Hate jQuery too ;D
Dan
@treeface: CodeIgniter's UserGuide says: "Models are PHP classes that are designed to work with information in your database." - in this context your statement can be true. However I understand model in quite a different way - "Model is a place to implement all business logic of you application". This is very, very general and it's natural that frameworks can't have support for such a general task.
Tomik
Sure, Tomik, but CI is flexible enough to allow for keeping all of your business logic inside of them. I've made CI applications where controllers only direct traffic between the models and views without performing any real logic on the data.
treeface
@Dan Indeed I do :)
Magnus Nordlander
A: 

I highly recommend the CodeIgniter from scratch series on Nettuts+.

http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-profiling-benchmarking-hooks/

EDIT: @Galen

This is not correct. CodeIgniter supports PHP 5.3.0 since version 1.7.2.

Octavian Damiean
CodeIgniter works on PHP 4.3 which means it still uses old and limited PHP4 object model.
Tomik
+3  A: 

Code igniter is php4, i don't recommend it too much. PHP OOP has changed since then. A better idea would be kohana

I learned a lot by checking out Konstrukt. The creator is semi active on SO too =)

Galen
+1  A: 

Magento Commerce has complex multi layered models (www.magentocommerce.com) which you may pick up a trick or two from.

But my (personal) experience with Magento was frustrating: I was hacking with it around version 0.8-1.1 and it appeared over engineered and poorly documented. Trying to figure out how it actually worked was complex: you'd end up with tons files open in your editor, most of them called Abstract.php. Hopefully things have improved a long way since then.

Steve
Hi, thanks for suggestion, this really could be good.I think that "you'd end up with tons files open in your editor, most of them called Abstract.php" is a price you need to pay if you want good design. Unfortunately, good OOP designs are usually not trivial.On the other hand, complexity doesn't guarantee quality! :D
Tomik
A: 

If you're looking for "excellent OOP design" you're probably looking into the wrong corner. PHP is not very strong at it's OOP patterns. If your study and situation permits I recommend you looking into a Java project instead.

schuilr
People keep telling me this, however they fail to provide a reason.So, what essential OOP feature PHP misses?
Tomik
flamebait? plenty of examples of patterns in PHP on the web (google for "php patterns"). Since 5.3 the language is very capable. I think its more about the skill of the programmers in a project rather than the language itself that influences excellence in design...
Steve
A "more pure" OO language does not enforce an excellent design. That is up to the designer. And I do not know when you've last taken a look at PHP, but as of version 5, its object capabilities are fairly strong.
rr
-1. PHP's OO design in based on Java's, and is pretty damn close in 5.3. If you think PHP has crap OO support, you are a moron.
Coronatus
The OO support in PHP is pretty good. That said, most (but definitely not all) "object oriented" code in PHP is crap.
Magnus Nordlander
For example, PHP does not provide much standard interfaces such as e.g. a Map which makes it harder to reuse classes from other projects. Also, when doing proper OOP in PHP the overhead of including and reinterpreting all the interfaces and classes becomes significant. An op-code cache helps only so much for that, but doesn't really solve the problem. Like Coronatus wrote: PHP OO design is based on Java's. Java focusses on OOP patterns more than PHP does (PHP has 'good' support only since 5.3). (fyi i have 10+y experience in PHP, 3+ in Java)
schuilr
@schuilr: as for Map, SplObjectStorage should be something like that. But your point could be true for other things - SPL is not very complete. I think we're talking about PHP language, not PHP interpreter. Speed is not valid point for me. I basically agree that OOP in PHP is not excellent but it's good enough.
Tomik
+1  A: 

I recommend you take a look at Symfony 2.

It is probably the best designed PHP project you'll find. It's SOLID, DRY and uses patterns where applicable.

Magnus Nordlander
Symfony is great, and check out it's sister-project Doctrine 2.0.
Coronatus
+1  A: 

What exactly is the problem you are facing with "Models"? You talk about Dependency Injection, but that really has nothing to do with the concept of a "Model" in the MVC context. If you are looking for examples of how to manage and pass dependencies down object hierarchies, you might want to take a look at Symfony 2.0's Dependency Injection Container implementation.

rr
I know Symfony DI Container, it's quite good. But what I want to see is some DI implementation IN ACTION. For example, even with complete DI Container you still need to solve the problem of injecting the container itself. What has model to do with DI? You need DI for quality implementation of multilayered models.
Tomik