views:

6765

answers:

14

We are starting a new app and are debating the benefits of MVC (using CodeIgniter for PHP) vs. using object oriented PHP.

I myself only really started using object oriented PHP, and do like it, but should I use that over codeigniter? I see benefits of both.

+1  A: 

I would recommend an MVC framework, it really helps the enforcement of good design, and imo speeds up development.

pure PHP will work and is good for small to medium projects, but without a organized MVC framework, I find it easy to lapse and start slinging PHP code everywhere...

I have never used CodeIgniter (I used the Zend Framework) but I would certainly go the MVC route...

Thats my 2 cents.

mmattax
+23  A: 

They're not mutually exclusive - you can do MVC in an OO language.

Sietse
+1  A: 

CodeIgniter is actually an object oriented framework. You can do MVC with it, but you're not required to. It's still a good base for building an application with, regardless of whether you use the MVC components or not.

Jeremy Privett
+5  A: 

CodeIgniter is highly Object oriented so there really isn't much different! CodeIgniter is great as you get a lot of existing libraries for stuff you don't have to code again, e.g form validation, calendar,etc. You also get search-engine-friendly URLs in the format:

www.site.com/class/function/variable

which I find really productive.

Hope this helps!

+17  A: 

As mentioned by others, MVC and OOP are not mutually exclusive. In fact, most good MVC frameworks are object oriented. I would highly recommend checking out the Zend Framework as well.

inxilpro
Except MVC isn't really an OO pattern.
Dave
@Dave how is that? There almost always something like inheriting from a Model which provides easy ORM. How is this not using lots of the OO principles.
Sander Versluys
@Dave: I second Sander's question, how do you go about implementing a non convoluted MVC without OOP?
mike
+2  A: 

Using the Model-View-Controller pattern is a great idea. Before you dive into that consider the Presentation-Abstraction-Control pattern, which is further development on MVC for more complex interfaces. In any case, separating out the layers will have a number of benefits.

I'll go against the popular grain when it comes to OOP. If you aren't comfortable with OOP you don't need to use it. You can still write a MVC setup procedurally. If you are comfortable with OOP, by all means, go for it. Some great apps have and are still written procedurally. Still others are OO.

If you are going to use a framework check out CodeIgnitor, Symfony, or Zend. Others are good. These three are pretty fast and give you a variety of options. CodeIgnitor is really lightweight and barely a framework. Symfony is a good MVC framework. And, Zend is give you the ability to architect your own structure and provides the most freedom.

If you aren't much of a coder (and that's ok) or are looking for something that does a little more for you check out drupal and Joomla!. They are content management frameworks and do a good job separating the layers like the straight up frameworks do but may be easier to use for a non-coder or someone looking to code less.

Go with what's comfortable but try to use a separated layers (MVC or PAC).

Matt Farina
Thanks, I am a coder (thank god!) and have used PHP just haven't started from scratch often. Most recently I have been using Mediawiki, thus learning the OOP aspect, but have been interested in trying one of the frameworks.
Adam Lerman
+1  A: 

I'm not sure what you mean by "object-oriented PHP", but CodeIgniter is a very good framework to quickly create web applications. It certainly beats writing your own object-oriented framework first, and then using it to write your application...

And, as stated above, CodeIgniter is object-oriented.

Christian Davén
+3  A: 

You can use both at once, indeed I do at the moment.

I have found the MVC is very useful as it means that designers and developers don't step on each other's toes so often.

Object orientated programming for me makes the code neater and easier to understand.

Teifion
+1  A: 

CodeIgniter is not what I would call a full-stack MVC implementation, but more a collection of useful libraries that can be used together to produce an MVC architecture.

If you're interested in 'complete' Model-View-Controller implementations in PHP, I would take a look at CakePHP or Symfony.

jperras
+4  A: 

Kohana is a fantastic port of Codeig that has a great ORM layer.

Buzz
+1  A: 

separatign view from data has been practiced since long before mvc word was introduced. even without mvc, you can create class libraries and create php pages which include required classes and call appropriate class methods. I don't prefer making separate files for views.

mvc is just a fancy name of separating of data access layer, processing page and layouts. I have seen many good non-mvc architectures. On web I believe its not necessary to have single page handling all page requests(common or front controller). You can create common include file and do common stuff right there.

A: 

MVC or object oriented?

If you're not sure of the difference between MVC & object orientation, and "which would be better", this is what I would recommend:

  • First, look at a few "set it up and get going" tutorials for the CodeIgniter/Kohana, Zend, CakePHP, ezComponents, and Agavi frameworks.
  • Next, take note of what they have in common, and what some lack
  • Then, make Pros & Cons lists for each (including things as trivial as "I don't like the syntax XYZ Framework uses)
  • Lastly, try to build your own (simple) framework, incorporating some of what you like from the others, and none of what you dislike. This will help you come to a deeper understanding of MVC, object orientation, and what frameworks (in general) have to offer.

Now you're in a position to choose which one to use. Don't use the one you build; you'll just end up re-inventing many wheels, and wasting a lot of time. Stand on the shoulders of giants, and take advantage of what entire communities have built FOR YOU. That's why they exist.

lo_fye
Not a big fan of MVC but I think your suggestions are very very sensible - research thoroughly first and then make an informed decision.
Dave
+1  A: 

Although it hasn't been mentioned, a framework which proves to be faster than CodeIgniter even if it is at its beginnings, is the Yii framework.

It is faster and more lightweight compared to CodeIgniter because it takes advantage of the OOP features in PHP5 - look at the benchmarks, and it comes with a full MVC support and other components.

As for the "OOP vs. MVC" debate, I can only say that MVC code that is not OOP is usually poorly designed - actually I've never seen a procedural MVC implementation well written and widely used.

Flavius
+1  A: 

MVC doesn't always have to mean using CodeIgniter, Zend, Yii, etc. MVC is just a pattern for code separation. You can achieve MVC with the code below as your bootstrapper then use includes inside your controllers.

$g=$_GET;$c=@$g['c']?$g['c']:'Home';
if(!@include"c/$c.php")die('fail');
$m=method_exists($c,$m=@$g['m'])?$m:'index';
$o=new$c;$o->$m($g);

Explanation of my madness

This was done as an experiment to see how light I could make a system that would run a CodeIgniter-like controller system.

Phil Sturgeon