SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Post Completely OOP - or just a bit??

    Hey everyone.

    I am finally getting round to building my own site, which will mainly be a portfolio / client-area site. I have written in OOP enough to be comfortable programming with it, but I'm not sure if a whole site should be OOP.

    The reason Im going to be using OOP is that I'm building a collection of classes which can be glued together easily to create the back-end of websites.

    I know that there are a few downsides to OOP, the main one being that it can be quite slow. But there are also many upsides to it, such as ease of re-use.

    So my question is: Is it worth creating the whole back-end of a site in OOP php?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it is. No, OOP is not slower than procedural.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    GR
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make everything static functions and it should be fast enough.

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quite slow?
    A few hundred ms (at most) won't have any real world effect on your server or the perceived responsiveness of your site.
    That is the most minor concern when choosing between Procedural or OOP.

    If I were you I'd at least draft all the business logic into classes to separate it from the general flow of your code, and the presentation.

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My sites, all running on the zend framework, which is a completely OOP framework loading a dozen classes every request, is quite responsive I wouldn't worry about speed.

    As always I would recommend you do what is easier for you. But I recommend full OOP for the reason that it is easy to extend and reuse for future sites.
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  6. #6
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by _Steve_ View Post
    Make everything static functions and it should be fast enough.
    By making all methods static you loose the whole point of OOP.

    Quote Originally Posted by arkinstall View Post
    I know that there are a few downsides to OOP, the main one being that it can be quite slow. But there are also many upsides to it, such as ease of re-use.

    So my question is: Is it worth creating the whole back-end of a site in OOP php?
    Yes, if you want to go OOP way, use it from the ground up. I would also suggest you looking into existing frameworks, such as: CakePHP, Symfony and PRADO.

  7. #7
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    GR
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by earl-grey View Post
    By making all methods static you loose the whole point of OOP.
    But you will have to ask yourself what is oop? How does oop applies to the WEB?

    I'll let you answer it and I tell you a oop example but not for web.
    I'm creating an animation and I need some rectangle sprites. Those sprites are derived from the class sprites_rectangles which derives from the base class sprite. So instantiating many different rect sprites objects are esential because each one has different properties.

    Unless you are falling into the category where many objects are derived from
    your class, I prefer static classes and singleton patterns for web apps.

  8. #8
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by _Steve_ View Post
    Make everything static functions and it should be fast enough.
    Sorry, but that is complete rubbish advice. Static methods has nothing to do with OOP.

    Quote Originally Posted by _Steve_ View Post
    But you will have to ask yourself what is oop?
    Decoupling through polymorphism is a huge part of it. Static references completely subverts this feature. Static methods basically suffers from the same problems as global variables does.

    Quote Originally Posted by _Steve_ View Post
    How does oop applies to the WEB?
    Pretty much the same as to programming in general.

  9. #9
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    GR
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    About static methods I wasn't referring to oop rather to designing an application.

  10. #10
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by _Steve_ View Post
    But you will have to ask yourself what is oop? How does oop applies to the WEB?

    I'll let you answer it and I tell you a oop example but not for web.
    I'm creating an animation and I need some rectangle sprites. Those sprites are derived from the class sprites_rectangles which derives from the base class sprite. So instantiating many different rect sprites objects are esential because each one has different properties.

    Unless you are falling into the category where many objects are derived from
    your class, I prefer static classes and singleton patterns for web apps.
    kyberfabrikken has already said more than I'd say on your questions, so I won't duplicate his words.

    Singleton has very few proper uses, and usually should be replaced with simple passing of an object as an argument or using Registry pattern.

    Using static classes for everything is much like grouping functions into namespaces. It doesn't make your application object-oriented.

  11. #11
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OOP is extremely useful on the web. Web applications are integrated, updated, upgraded and changed far more often than most of their desktop counterparts. This make encapsulation, re-use and maintainability all important.

    In your case, deadlines aren't an issue, but if you start doing this sort of coding for clients, web application deadlines are many times measured in days or even hours. Having a good toolbox of stuff, let alone a framework, will make these situations far less stressful and in some cases make you someone's hero.

    I agree with the idea that static classes help with namespacing basic functions that don't need to act together. Already had a debate on that however, don't want another one.

    I will say, in answer to the question of "what is OOP?", that I wouldn't get too bogged down in that discussion. There are, just as there are in many areas of life "purists" (to use a nice word), that are under the impression, that if you don't use every "approved" feature of "true" OOP, that it really isn't OOP, and either you are an idiot or some kind of heretic.

    If it's not outrageously insecure, inefficient, unmaintainable or hard to read, and it works for you, then go ahead. Also, even if you are just doing a contact form for someone else for instance, it doesn't mean you have to revert back to procedural coding. A good form validation, spam blocker, email sending class combo can save you huge amounts of time. You don't need a full blown MVC for that, but you can still use OOP.

    I haven't noticed any slowness with OOP. Yes many times it's more verbose, but for good reason. That doesn't mean it's slow, that's a myth. Poorly designed applications are slow.

  12. #12
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I doubt anyone will agree with me, but I still don't understand why people have such a fascination with OOP. Don't get me wrong, it's an amazing concept, and very useful in many circumstances. But it just seems that many developers use OOP because they think they should, without any reasoning behind it. *shrug*

    I still use procedural programming all the time, and it works great. I also use OOP all the time, but only when it makes sense to do so, and will provide benefit over procedural. I've seen lots of code where OOP was way over-used for almost no reason, and for the most part, only complicated the application far more than was needed. For example, for template parsing of course OOP is the way to go, but for things such as say, manipulating a string of data into a certain array, you don't need a separate class for that.

    For a good example, PHP isn't an object-orientated language, and they've done just fine.
    Kiopa Software -- Demo Now Online! Check it out!
    Goal: Consolidate all data & tools you use on a daily basis.
    Grand opening special, licenses FREE for a limited time.

  13. #13
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RDFrame View Post
    I doubt anyone will agree with me, but I still don't understand why people have such a fascination with OOP. Don't get me wrong, it's an amazing concept, and very useful in many circumstances. But it just seems that many developers use OOP because they think they should, without any reasoning behind it. *shrug*

    I still use procedural programming all the time, and it works great. I also use OOP all the time, but only when it makes sense to do so, and will provide benefit over procedural. I've seen lots of code where OOP was way over-used for almost no reason, and for the most part, only complicated the application far more than was needed. For example, for template parsing of course OOP is the way to go, but for things such as say, manipulating a string of data into a certain array, you don't need a separate class for that.

    For a good example, PHP isn't an object-orientated language, and they've done just fine.
    Unless of course you want to group multiple string manipulations into a single class that is highly configurable and re-usable. Why keep coding the same things over and over and over again?

    For instance a good HTML table class can make it so you never have to write table code to display tabular data from a table again. Just feed it the recordset as an array and let it do it's thing. This isn't just about frameworks. As I stated in my previous post, if you want to do just a contact form, yes, you don't need a full blown MVC setup for that. In that case you are talking about re-usable parts and tools. It has saved me huge amounts of time.

    Bloat and unnecessary complexity isn't the sole purview of OOP apps. If you ever get a chance, take a look at the source code for VTiger CRM. It's a bloated, steaming pile of crap. It's mostly procedural with a half baked use of OOP and 4000 line function pages that have code in them that should be localized in module classes.

    The Javascript would make you run screaming into the night. Even where they do use classes, even the base class is hard coded with if module this and if module that. The global keyword is used profusely. It's a god awful mess of an open source project and a black eye to PHP. Poor design is the reason, not one type of programming over the other.

    OOP has the advantage of re-use and maintainability. You can test a piece of it in isolation for easier debugging. Minimal use of procedural is fine but the more complex web applications get, the more OOP is needed.
    Last edited by Hammer65; Sep 2, 2007 at 22:46.

  14. #14
    SitePoint Member
    Join Date
    May 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a big OOP fan, I feel it makes code much cleaner and easy to modify and saves time. However, sometimes it is used un-neccessarily. I say, only use it if the function of the class will be needed in more than one instance or file. For example, if you were running a site that was coded in PHP as it had dynamic content, and you wanted to display the page generation time, then OOP would be quite usefull:

    PHP Code:
    <?php
    include "/path/to/classes.php";
    $page = new pagetimer;
    $page->start();
    /*
    Page content
    goes in here.
    */
    $page->finish();
    echo 
    $page->gentime;
    but if you had one page that displayed a user's profile, and no other page / part of the file outputted the profile, I don't think it would make sense to OOP it:

    PHP Code:
    <?php
    include "/path/to/classes.php";
    $profile = new profile($id);
    echo 
    "Name:".$profile->name;
    echo 
    "Age:".$profile->age;
    echo 
    "Occupation:".$profile->occ;
    echo 
    "References:".$profile->refs;
    ?>
    [/
    but to some it would certainly make the code of the profile page file look cleaner, and would therefore be worth it. It's all a matter of opinion. There's no right and wrong answer.

  15. #15
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RDFrame View Post
    I doubt anyone will agree with me, but I still don't understand why people have such a fascination with OOP. Don't get me wrong, it's an amazing concept, and very useful in many circumstances. But it just seems that many developers use OOP because they think they should, without any reasoning behind it. *shrug*
    Desire to learn, begins with fascination. It's impossible to know exactly, what you're getting into beforehand.

  16. #16
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RDFrame View Post
    But it just seems that many developers use OOP because they think they should, without any reasoning behind it. *shrug*
    Even more developers use OOP without knowing of this. OOP is a methodology, not the syntax, and it is so common because it adequately describes real coding practices. Most programs are OOP, even if they don't use 'class' keyword.

    I still use procedural programming all the time, and it works great.
    I don't think you do. What is often considered "procedural" (e.g. most of php runtime library) is actually object oriented by the nature.


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
  •