Page 1 of 1

Difference advantages of using controller and classes

Posted: Tue Jul 20, 2010 6:25 am
by krsm1980
Hello forums,
I am trying to figure out difference b/w classes and controller.May you tell me the advantages of using these instead of simple PHP pages
Regards

Re: Difference advantages of using controller and classes

Posted: Tue Jul 20, 2010 6:12 pm
by josh
What is the advantage of dividing a novel down into "parts" and "chapters"? Makes it easier for human beings to consume your "book" (or program)

Re: Difference advantages of using controller and classes

Posted: Sat Aug 07, 2010 8:13 am
by MindOverBody
plus, if coded correctly, oop code is much more reusable.

Re: Difference advantages of using controller and classes

Posted: Tue Aug 10, 2010 5:10 pm
by Gargoyle
plus, if coded correctly, oop code is much more reusable.
sorry - nonsense.

I'm not a big fan of OOP, but implementing the MVC principle is certainly a good idea.

Re: Difference advantages of using controller and classes

Posted: Wed Aug 11, 2010 11:00 am
by josh
He's right though, OOP allows for more patterns of re-use. This can be measured scientifically with software metrics.

Re: Difference advantages of using controller and classes

Posted: Wed Aug 11, 2010 11:20 am
by Gargoyle
you can't prove anything with software metrics because it totally depends on the parameters defined.

but let's keep it on topic.

Re: Difference advantages of using controller and classes

Posted: Wed Aug 11, 2010 5:25 pm
by alex.barylski
Gargoyle wrote:you can't prove anything with software metrics because it totally depends on the parameters defined.

but let's keep it on topic.
Counting SLOC is a single quantifiable metric. As a lone metric, it doesn't prove much, but when you collate dozens or hundreds of other metrics, you begin to paint a much clearer picture.

Cheers,
Alex

Re: Difference advantages of using controller and classes

Posted: Wed Aug 11, 2010 5:53 pm
by Christopher
There were two questions, I don't think either was answered.
krsm1980 wrote:I am trying to figure out difference b/w classes and controller.
Classes use the "class" construct. However, you can create code using the class construct and still just call it procedurally. That is a common mistake that people make when starting with OOP. Creating object instances of a class is the proper way to code (and yes, there are always exceptions).

A Controller is a specific piece of code that deals with request processing and program flow, and providing support code to deal with those things (e.g., forwards). There are three Controller patterns related to this discussion.

1. The Page Controller pattern is what it sounds like you are using right now -- that is what is usually called "simple PHP pages" where each page is a specific entry point for the application. Typically each Page Controller includes common code.

2. The Front Controller pattern is where all requests to the application go through a single entry point (PHP script). The has the advantages of centralizing code that is duplicated in Page Controllers (especially Access Control). Usually the Front Controller loads and runs (i.e, dispatches) and Action Controller that contains all the page specific code that is in your Page Controllers, but none of the common code which are provided centrally (by the Front Controller, etc.

3. Action Controllers usually follow the Command pattern. The goal is to have them fairly minimal and just focus on the code related to a specific request. Common code is provided to them and therefore can be refactored in a structured way.
krsm1980 wrote:May you tell me the advantages of using these instead of simple PHP pages
Hopefully reading the above will give you some insights into the advantages. In general, these patterns are solutions to problems that occur as applications grow and become more complex. For small websites there is not much that can beat whipping up some "simple PHP pages."

Front + Action Controllers are a common implementation of the Controller part of the Model-View-Controller pattern. That pattern specifies that the Controllers stay focused just on request processing and program flow. The View is focused on only building the response that is sent back to browser. The Controller and View are together in what is called the Presentation layer. All the "business" or "domain" code is separated out into a separate lower Model layer. The goal is to keep code in the Model layer independent from the request/response and program flow code. Typically in PHP each Model, View and Controller is a single class in its own file. This scheme has proven to make larger, more complex application easier for multiple programmers to maintain.

Re: Difference advantages of using controller and classes

Posted: Wed Aug 11, 2010 6:12 pm
by Gargoyle
Counting SLOC is a single quantifiable metric.
right. but you're making the mistake to assume that this metric can actually be compared directly.

Re: Difference advantages of using controller and classes

Posted: Thu Aug 12, 2010 3:26 pm
by Sephern
Classes and Controllers are different things.

Classes are an object orientated construct. Whether there's any advantages of using Object Orientation over procedural code is really a matter of opinion (and debate between programmers).
Personally, I think OO is the way to code, especially for mid sized projects upwards.
In my opinion its
  • Easier to create reusable code
  • Has a much greater deal of syntactic sugar(making it easier to understand by others)
  • The ability to use various design patterns (Singleton, Adaptor, Factory etc. If you can use them in procedural code, then I'm sure it's not as simple as in oo)
  • Maps to real world applications a lot easier
Obviously, this is all just my opinion. I find making a class for users in my website, for example, a really natural way of doing things, and by using constructors and such it just really makes coding that bit easier (in my opinion).

Controllers are to do with MVC Architecture (Model, View, Controller). The advantage of structuring your app like this is that you split it up into smaller, reusable segments which all have a specific job to do (rather than coding the same thing multiple times).
In essence, you have the Model, which is the part that does all your logic and data stuff. If you're fetching, writing or deleting something to/from a database, the chances are you'll be using a model to do it (or should be). The same with files, external APIs. This should do all the heavy lifting of your website.

The controller is the part that figures out what goes where. It determines which models need to be called, and calls them. It then determines which view needs to be called to display the data, and passes the data between. One controller can serve multiple views, and be served by multiple models. A controller can also be responsible for stuff like parsing scripting languages in views (so people writing skins for your application don't need to know PHP - think vBulletin or something similar).

The view is the part that the user sees. It can be a webpage (and usually is), but also a PDF document, an RSS feed, or anything else you can think of. Typically data is passed from the controller into the View, which renders and displays the content to the user. Each segment of the view should only be coded once (so you'll be including the header, footer, etc), and you might want to implement a scripting language to deal with the view if somebody with no coding knowledge is making skins for you. The scripting language would be parsed in the controller, as aforementioned.

Re: Difference advantages of using controller and classes

Posted: Sat Aug 14, 2010 4:07 pm
by josh
Christopher wrote:There were two questions, I don't think either was answered.
I was answering the latter question: "what is the advantage". To some one who is new, a lot of the technical terms make no sense and even turn a lot of people off from what is really simple & obvious. Some one told me I would get a single entry point to my application. I didn't see the advantage in that though until a year later when I learned it would make code easier to manage. I think that is what it comes down to at the end of the day, is it going to make your job easier or harder.

I left the other question unanswered intentionally, looks like you answered it thoroughly. I think its important people get the bottom line though. There's lots of crazy people claiming their techniques are the best. The bottom line is what each technique does to your bottom line. MVC (model-view-controller) does a lot of benefit to it - because it "de-couples different types of logic" - but you see most people aren't going to understand the part in quotes or why that is good, until they just try it, hence my simplified (perhaps even incomplete, or technically inaccurate) answer ;-)