SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question What's the advantage (class controllers)

    Hi,

    I was wondering, what's the advantage of having following code

    PHP Code:
    class Controller_Whatever extends Controller
    {
       public function 
    index()
       {
       }

       public function 
    read()
       {
       }

    instead of simply use a regular file with a regular top2down (procedural) code for controllers, per file (./whatever/index.php, whatever/read.php etc).

    Sorry if this is a stupid question, there's probably an advantage since everybody's doing it, but I just don't see it!

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The advantages of OOP come from how it is used. Badly written OO code can be worse than mediocre procedural code. In fact very well written procedural code can be a lot better than sloppy OO. I'm afraid your example is one of those, no wonder you get the doubt.

    Check out the uses of a Controller in design patterns, you'll get the point. The most common use is MVC. The controller plays its part as user request handler for a particular action. Although you can always achieve similar effect in procedural code, it produces a much better organized code nonetheless.

    If that's not enough, the Chain of Responsibility is a perfect example. It talks about a chain of controllers (well commands in fact, but their function is pretty much the same), where each controller can pass the request handling to the next controller in the chain. That can save you loads of conditional blocks as opposed to procedural code. Imagine a chain: article.view.edit. You get the drift.

    Those are the most common uses, but there's more; do some research, you won't regret.
    Saul

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your respons. I am reading up as of now, but do you maybe have or could point to a good code example that I can read. I tend to learn better that way..

  4. #4
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OOP encapsulates data with behaviour rather than passing data around to lots of functions. It allows different kinds of meaning to be separated cleanly into discrete components. This helps make code easier to work with. The internals of a component are isolated in a class scope and so can be edited without affecting any other part of the program. New behaviours can be introduced easily simply by swapping different components which implement the same interface. Bringing data and behaviour together allows you to define new concepts matching the way you think about the domain in a way you simply cannot do in procedural.

    Basically it's just a much more powerful way to work which adds a new dimension to your programming. It does take a while to learn though. I think everyone finds it hard at first but eventually it will start to sink in.

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Saul

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff View Post
    Basically it's just a much more powerful way to work which adds a new dimension to your programming. It does take a while to learn though. I think everyone finds it hard at first but eventually it will start to sink in.
    Well, I pretty much understand the reason, or the idealogy of OOP. But I have trouble seeing the code infront of me. For instance, I'd like to use multiple controllers depending on what I want to do, even if it's one page, how do I make such an controller Anyhow, I just have to read more examples, I'm buying a book next week, hopefully that will help.

    Thanks

  7. #7
    SitePoint Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fl0w View Post
    Well, I pretty much understand the reason, or the idealogy of OOP.
    It's more than that. Some people say it's a way of thinking and I tend to agree with them. However, you must be able to understand that way of thinking (call it meta-thinking or whatever you want). You must know that applying Liskov substitution is the Right Thing™ to do even though you might apply it intuitively. You have to be aware of the problems design patterns try to solve an the means the use to achieve that so you can apply those means to your own products.

    Studying object oriented design is really a lot like studying philosophy. There is a general consensus of good and bad but everyone will give you different results based on that duality. Some people will say that there is no duality because good and bad are antique concepts that should be ignored. It's your task to find your way though the OOP rules, practices, patterns and evangelists to find your path (and write papers about it if it works and is completely different to what everybody is doing :P).

    Anyway, really understanding OOP will be a long process (there is a lot more to it than 'getting encapsulation').
    Design patterns: trying to do Smalltalk in Java.
    I blog too, you know.

  8. #8
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jasper Bekkers View Post
    [...] Studying object oriented design is really a lot like studying philosophy. [...]
    And in the end of the day it opens more questions than answers. Couldn't agree more with that.
    Saul


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •