SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2000
    Location
    Yeppoon, Australia
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Having done some reading about OOP in PHP I was all ready to jump into it for a site then came across quite a few posts at phpbuilber.com saying how OOP adds huge overheads to pages and slows things down on the server quite a bit.

    I am well aware of the modularity and code resuse, etc advatanges of OOP from some C++ work however now I am stumped as to whether I should use them for a website or just put functions into include files and go that way for the basis of the site.

    So, now that I am thoughouly unsure of what to do I will ask, how do YOU guys do it? What are the best methods you have found?

    As always, assistance is appreciated
    Knowledge is knowing that a tomatoe is a fruit; wisdom is not putting it in a fruit salad.

  2. #2
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The site I'm developing at the moment makes extensive use of OOP, and I have to admit I'd never heard anything about it having severe implications for performance. Could you paste a URL to the post about this please?

  3. #3
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I haven't used OOP in PHP, however, I will make this gross generalisiation An object oriented design is going to, in general, require more code and time to develop, and will be less efficient when compiled and use more memory. Gross generalisation! but in my experience true. Of course the advantages lie in modularity (encapsulation), security (although PHP doesn't have data hiding), reusability.

    In object oriented languages such as C++ or VB you have a minimum of two function calls for every object instantiated; one call for the constructor, and one call for the destructor. As well, every time you want to instantiate an object, all its data members are created in and held in memory even though they may not all be required. So careful design is very important.

    Last year I worked on an application that was written in VB using OOP and the classic three tier structure. It was a nightmare, and at the end of the day we ended up decomposing the OO purity of the design because some of our use cases ended up instantiating hundreds (if not thousands) of objects (which themselves contained other objects) when the application only needed to know the value of one or two data members of each object, which was sitting in a relational DB anyway!

    I would be interested to hear other's views on OOP in PHP.

  4. #4
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd disagree on the more code thing - so far I've written quite a lot of the background code for my new site project using OOP, and it's saved me a huge amount of work. I'm using a Base class which can talk to a mySQL database table, then extending that class to create new classes with specific details of individual tables (the table name and what fields it has in it). As a result I can do the following:

    $user = new User("id = '5'");

    That will create a User object and load the details for user number 5 from the database (the id = '5' bit gets used as the WHERE clause).

    Then I can do this:

    $user->set_var('password', 'newpass');

    Which changes the password variable in the object to whatever I specify

    Finally I do this:

    $user->save_update("id = '5'");

    That updates the user record in the database with the new information. As you can imagine, this saves loads of code when I'm editing items in the database etc. The best part of it is that the code for the User class is extremely simple - it inherits all of the complex functionality / methods from my Base SQL class.

    This is saving me a huge amount of time and effort in the coding - I agree with what you are saying about creating thousands of objects just to get at a proeprty that's already stored in a database, but I'm using normal procedural code as well so I can cheat and just grab it straight away. It's not pure OOP at all, but the hybrid style seems to be giving me the best of both worlds.

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with Skunk and freakysid on this one. I have developed several backend administration systems, one of them was written with scripts scattered throughout many files and worked fine, and was fast. Then I used OOP for one, the whole admin section was self-contained in one class file, I did notice that while it was cleaner and easier to update, it did run a bit slower. But then again, I just finished a hsting signup section for my company, again using OOP, sessions and extensive use of phplib's template class. Now this one was a dream, it works great, is fast and all using one class file. I think what made this one run faster was the use of templates instead of using php to generate all the html.

    I have removed the link, because inadvertably I think I am spamming the forums, all I really wanted to do is show the functionality of it, but some may percieve this as advertising for my company's hosting.
    Last edited by freddydoesphp; Apr 23, 2001 at 14:23.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Enthusiast Jujubee's Avatar
    Join Date
    Mar 2001
    Location
    Canada
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anyone post a newbie link to an OOP explanation?

    I've heard of it, but all the class stuff is new to me. I'm all for saving time though.

  7. #7
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a good definition of OOP, jujubee: http://www.dict.org/bin/Dict?Form=Dict2&Database=*&Query=object-oriented+programming .

    I'm an OOP freak, personally. While it might have taken more time to code in the beginning, I have reused methods and even whole modules, resulting in a dramatic decrease in development time. I use Perl mainly because I feel its OOP is the best of all interpreted language.

    As Skunk said, with OOP you can do things like this (rough example, it'll probably be correct Perl):

    $file = new File("/www/$topic.page");
    $oldcontents = $file->contents;
    $file->contents = 'lala';
    $file->save;
    $file->close;

    Compared to:
    open(PAGE, "/www/$topic.page");
    undef $/;
    $oldcontents = <PAGE>;
    $/ = "\n";
    close(PAGE);
    open(PAGE, ">/www/$topic.page");
    print PAGE 'lalala';
    close(PAGE);

    Which is more obvious? The first one, by far. Which is less bug-prone? The first one, the OOP one.

    At the very least you should make a .inc.php or .pm file with a ton of subroutines. When I was writing code for my old site, I didn't, and made the mistake of having to change each file (about 5) every time I made a simple mistake or wanted to, say, use a different SQL table.

    When it was first concepted, people thought of OOP as a joke (its acronym is somewhat ironic). After all, who would want to write much more code just in case they might reuse it? Back then, the emphasis was on speed and not the time it took for developers to make software. Now, the shorter the development cycle, the better.

    My $.05. Sorry, I was out of pennies, so you can keep the change.

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2000
    Location
    Yeppoon, Australia
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The article is here: http://www.phpbuilder.com/columns/mark20000727.php3

    and the post in question is, well it is actually several of the posts for the article.

    Thanks for your views, I think I will lean towards Skunk's omplementation of mixing the two - that seems to be the way it is most often done. Template's also seem to be interesting - I have read some good info on them. I might look at using them as well... but maybe later on
    Knowledge is knowing that a tomatoe is a fruit; wisdom is not putting it in a fruit salad.

  9. #9
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is an interesting issue. I admit I have not used OOP extensively, but I've used it here and there, and seem to have a handle on the basics. I must say: I don't like it much.

    If you do what I do, and spend most of your time simply working on web applications, then you won't really have much need for OOP. When using PHP, I find functions take care of the job with almost no difficulties.

    I would imagine (and this is only an educated guess, mind you) that OOP would come in handy with some larger programs and such. For web projects, and quick applications, I see no use for it.

    Quinn: your example obviously demonstrates cases in which OOP can be useful, but the choice is not OOP .vs. plain old commands -- I'd imagine things could be done just as efficiently using a function or two.

    I will say this: despite it's uses, it is not a God-send.

  10. #10
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $file = new File("/www/$topic.page");
    $oldcontents = $file->contents;
    $file->contents = 'lala';
    $file->save;
    $file->close;
    is about as fair as me doing this...
    <?
    include("functions.inc");
    dofile($file,$contents,1);
    ?>

    "Which is less bug-prone? " ? who knows - until we see the class or function behind it we never shall.

    I [b]know[b] that that is only an example, but people use OOP for far far less!

    basically you not only have to write the class, but also call it and its methods, in the example above the call would probably be bigger than the class itself.

    OOP by its very nature has to take a performance hit, luckily PHP zips along fast enough for this to be negligible for well written code.

    I also can not understand peoples hammering the reuseability issue.. so you cant include/reuse a function?

    When all is said and done perhaps OOP has its place I just think its overuse is bit daft, and with regard to the performance hit, however small - why (in most circumstances) would you?

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    firepages, that poses an interesting issue, don't mind me, I am naturally biased towards OOP, but let's look at it this way.

    $file = new File("/www/$topic.page");
    $oldcontents = $file->contents;
    $file->contents = 'lala';
    $file->save;
    $file->close;
    vs.

    include("functions.inc");
    dofile($file,$contents,1);
    Like you said it qwill be hard to compare without seeing the code, and I am probably a complete when it comes to the theory of OOP, but I will give it a shot.

    Let's start with the first example, it appears a new instance of the class File is instantiated and a private class var is created holding the path and name of the file to work with. So right there we cut out having to globalize any vars. So the method save() can use this variable and so can the rest of the methods without having to use global or haiving to pass like $file->save($file) because the class var is already present, it is about creating objects and then working with the objects. So in this case we create a new File object with the first property being the name and path of the file now we can use any of the classes methods to perfom tasks on this object like saving it or deleting it. This method seems to be a bit cleaner and easier to understand at least for me.

    Second example:

    The function dofile seems to handle everything and it seems to me that why even use a function at all, and in order to perfom multiple tasks on a singhle item, our function would need to have a lot of if...else... statements or a huge switch statement, so where does the overhead from the class surpass the overload of a crazy function with ten cases in a switch statement and a lot of extranaeous code.


    PHP Code:
    class File {
    var 
    file;
    function 
    File($file) {
    $this->file $file;
    }
    function 
    save()
    $fp fopen($this->file"w");
    fputs($fp$this->content);
    fclose($fp);
    }
    function 
    delete()
    unlink($this->file);


    as opposed to

    PHP Code:
    function dofile($file$content,$action) {
    switch(
    $action) {
    case 
    "save":
    $fp fopen($file"w");
    fputs($fp$content);
    fclose($fp);
    break;

    case 
    "delete":
    unlink($file);
    break;

    case 
    "showfile":
    fpassthru($file);
    break;

    }

    See where I am going with this?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  12. #12
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I may weigh back into this discussion ... rummaging around for two bob ... gee these forums eat through your small change, I'll be robbing the piggy bank next ...

    I think at one level, OO is may not be as relevent to writing server side scripts for dynamic web pages - its usefullnes increases as the scale of the system you are designing increases. OO Design and Programming has so many advantages as a complete methodology for analysis, design and construction of information systems. OO concepts and techniques aid in analysis (use cases for requirements discovery), the design of the system (class models, message-trace diagrams - aka system sequence diagrams- network design) and the construction phases (especially unit testing).

    But such discussion is beyond the scope of this thread and my experience. I just wanted to throw in that in IT, OO is not just about the code its a whole integrated approach to designing information systems.

    Now back to more practical considerations. I agree whole-heartedly with freddy about classes making reuse easier. Sure you can write a collection of functions, but as freddy pointed out encapsuliation means that all the necessary data (object state) is held inside the class, and all the functions are neatly wrapped up inside a specific class scope - so there should be no scope issues (I say should because I'm not too familiar with classes in PHP). So classes are the plug-and-play of programming. You grap a class or class library from the net, thow it in an include() function and start useing objects of that class without needing to worry about globals and scope issues. This encapsulation makes classes much more self documenting too.

    The thing that I think people overlook is that inheritence is a very handy way of extending or modifying a class to suit your speicific needs. If you can bother reading it a couple of days ago pedro_gb posted a problem which involved the phplib template class. pedro_gb, I don't know whether you solved your problem?!? Anyway, during that discussion, it dawned on me what pedro_gb was trying to do. Here's the link http://www.sitepointforums.com/showt...threadid=21469

    In essence he wanted to make Template class behave to suite a specific requirement of his script. Now the easy way to do this is through inheretence whereby you create a sub-class of the base class to either modify or extend the way it behaves. Very simple and very neat design!
    Last edited by freakysid; Apr 24, 2001 at 22:41.


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
  •