SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OOP vs. Old School

    I'm now starting to see where there are obviously up sides to OOPHP. However, there are also a few things that I don't understand as far as whats worth it. Things such as a quick sql query. I always see people use opendb / closedb includes to do it, I personally like doing the following:

    PHP Code:
    function sql($query) {
        
    $con mysql_connect("localhost","username","password");
        
    mysql_select_db("database"); // can also put variable here if dealing with multiple db's
        
    $sqlresult mysql_query("$query");
        
    mysql_close($con);
        return 
    $sqlresult;
    }

    $result sql($query "just throw the query in here"); 
    I was just reading a few chapters our of the SitePoint PHP Anthology and the amount of code to execute the same thing using his OOP method is huge compared.

    So what do you think of this method? And what else do you guys not bother with when writing an application in OOP?

  2. #2
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're just writing a small script, then I suppose OOP adds a small amount of extra code overhead. For small programs it really doesn't matter what you do as you won't be reusing the code anyway.

    In larger projects you actually write less code using OOP compared to the procedural equivalent. Benefits of OOP are less code, easier updates, and it being easier to understand.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  3. #3
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The procedural way for common things such as connecting to a DB just looks ghetto and too low-level, poorly implemented with not enough checks and such, such as your function and many other pieces of php out there. You're not even bothering to check if the result of the mysql_connect was a valid resource, nor validating or sanitizing the query - it just seems ghetto ( 2 minute chop up with not enough thought ) when compared to a more higher level class. I could do all that with my class via:

    $db->new Conn;
    $db->query('SELECT name from table');

    Etc. No idea where you came upon the impression that it's less code, because as bhanson has previously stated it becomes less code after a class is used.
    Cross browser css bugs

    Dan Schulz you will be missed

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kyle W View Post
    I'm now starting to see where there are obviously up sides to OOPHP. However, there are also a few things that I don't understand as far as whats worth it. Things such as a quick sql query. I always see people use opendb / closedb includes to do it
    personally, I would consider this bad coding practise, whether OOP or procedural.
    Create a class that opens the database connection in the constructor, and closes it again in the destructor, holding the connection as an attribute while it's instantiated.
    Then have a query method that you call, which uses the already opened connection.

    PHP Code:
    class database
    {
        private 
    $_connection NULL;

        function 
    query($sql) {
           return 
    mysql_query($query);
        }

        function 
    __construct($host,$username,$password,$database) {
            
    $this->_connection mysql_connect($host,$username,$password);
            
    mysql_select_db($database,$this->_connection);
        }  
    // function __construct()

        
    function __destruct() {
            
    mysql_close($this->_connection);
        }
    }  
    // class database


    $db = new database("localhost","username","password","database");
    $result $db->query("just throw the query in here");
    unset(
    $db); 
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    From my experience, writing OOP does in no way involve writing less code. In fact, in your early days you will write an inordinate amount of code extra compared to procedural.

    If you are trying to switch to OOP to write less code, don't.

    However, the code you do write will be more robust, easier to re-factor when the client asks for changes (and they will), extensible and if you've done it correctly, you may even be able to use it in many later projects without changing any code.

    Give this post a read, it's great and explains it much better than I ever could.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    From my experience, writing OOP does in no way involve writing less code. In fact, in your early days you will write an inordinate amount of code extra compared to procedural.

    If you are trying to switch to OOP to write less code, dont.

    However, the code you do write will be more robust, easier to refactor when the client asks for changes (and they will), extensible and if you've done it correctly, you may even be able to use it in many later projects without changing any code.

    Give this post a read, it's great and explains it much better than I even could.
    Well, after coding the initial class which is the hard part, which can be re-used on projects dozens of times it's much less than starting anew with low level code, much like writing a class several times for each project, it does become less work in the end.
    Cross browser css bugs

    Dan Schulz you will be missed

  7. #7
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SoulScratch View Post
    Well, after coding the initial class which is the hard part, which can be re-used on projects dozens of times it's much less than starting anew with low level code, much like writing a class several times for each project, it does become less work in the end.
    Exactly my thoughts. Then in the end, you could view it as not programming in PHP, rather your own little variation to it, your own little language.

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Every class you write is effectively one more collection of commands that you have available to use in all the future work you do in the language. By making all of the common processing that you always do into classes you reduce all of that code down to the one or two commands needed to use the class in every new project you do after the class was written and tested.

    The amount of testing for new projects will also be dramatically reduced since you already know that the classes work.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps off-topic, but:

    If you ask me, the term "old school" implies a certain venerability that procedural programming does not deserve. It's doubtful that procedural style would even exist if OOP had been invented first. Okay, that was facetious, but it amplifies my point, which is: You should get into the OOP habit regardless of how large your project du jour is. Once in the habit, it will make little sense in most cases to switch to procedural style.

  10. #10
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, just to add my two cents... procedural programming is not going anywhere soon. Obviously it is slowly going to be used less and less but novice programmers will probably still be starting with procedural + some low-level laguages like assembly or c don't have OOP capabilities at all (at least according to my limited experience with them... correct me if I am wrong) and these laguages are so to speak backbones of all higher ones.

  11. #11
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by risoknop View Post
    some low-level laguages like assembly or c don't have OOP capabilities at all
    The day that assembler is OOP will be the day Intel and other processor manufactures totally rethink their paradigm.

    And C does have OOP capabilities: it's called C++.
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  12. #12
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    There are at least five different programming styles of which procedural and OOP are just two. Just because OOP is currently the most useful for many projects doesn't mean that all the other styles of programming will cease to exist. Every so often I come across a project that is far more easily done using a rules based language that would require hundreds of time the code if a procedural or OOP language were to be used instead. Presumably there would be similar situations with all the other programming styles but those three styles are the only ones I actually know how to use properly and so anything that would be best written using any of the other styles I would have to write using one of procedural, OOP, or rules based unless I had the time to actually learn a more appropriate programming style and a language that supported it. Sometimes I wish that I actually knew more about logic based programming and constraint based programming so as to be able to more correctly choose an appropriate language for various tasks from all five programming styles that I know of rather thanhaving to choose from the three that I actually know how to use.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  13. #13
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mark Baker View Post
    The day that assembler is OOP will be the day Intel and other processor manufactures totally rethink their paradigm.

    And C does have OOP capabilities: it's called C++.
    Yeah but C++ is used for different purposes than C. CPUs, mobos etc almost all hardware and electronics is coded in Assembly or C, no OOP there and this isn't going to change.

    C++ is used mainly for desktop applications.

  14. #14
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Assembler doesn't have such things as procedural, object oriented, rules based, logic based, or constraint based variants as it operates at a lower level than that. Assembler uses a one to one relationship between program statement and executable machine code statements. To have a programming language support any of the five (or more) programming styles that higher level languages can use there needs to be a compiler that will convert from one of those styles back to machine code statements (or a byte code that operates at a similar lower level).

    The day Assembler can be used to write procedural code will be the day that writing code ceases to be necessary as the computers will be able to write their own code far more quickly and easily than we can once they are capable of understanding that level of complexity without a compiler to translate it for them.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  15. #15
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought it also qualified as procedural. At least it seemed to me that way when I coded few simple hello world programs in it. You have loops, you can use conditions (very primitive and limited but conditions nonetheless) etc...

  16. #16
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assembler doesn't have such things as procedural, object oriented, rules based, logic based, or constraint based variants as it operates at a lower level than that.
    It depends on the assembler you use:

    http://cs-netlab-01.lynchburg.edu/co.../OOPAssem.html

    The day Assembler can be used to write procedural code will be the day that writing code ceases to be necessary as the computers will be able to write their own code far more quickly and easily than we can once they are capable of understanding that level of complexity without a compiler to translate it for them.
    Procedural is a concept more than a technical enforcement. Just as functional programming and object oriented programming can effectively be emulated in any imperitive programming language.

    Look at Gtk...if memory serves me correctly it's implemented in plain C but emulates an object environment using structures, etc.
    The only constant in software is change itself

  17. #17
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    An assembler language is actually one that has a one to one mapping from commands to machine code and back again. Such a language is therefore restricted to the actual commands that the processor itself is set up to handle in the hardware. Such processing is at a far lower level than would normally be used in a procedural language. A simple statement in a procedural language such as a do while loop or function call would map to perhaps a dozen or more actual commands at the machine level. All conversions from one source statement to more than one machine statement requires the use of a compiler rather than an assembler to do the conversion. When talking about Assembler language you need to be very specific about exactly which processor the assembler language is for since an assembler language for one CPU will not produce code that can run on another CPU unless one of the CPUs uses a subset of the commands supported by the other and the code only uses that subset.

    There may be languages called assembler that do not have this one to one mapping with a particular processor but such a language is no longer a low level true assebmler language but is instead a high level language designed to be compiled. Only high level languages can be categorised as procedural, object, rules, constraint or logic based languages since the compile step will map whichever of those that the language uses into a series of commands that the processor can understand. A processor would need to support millions of different commands in order to be able to implement any high level structuring at the machine level and to be able to actually create its own machine commands to correspond to the ones you define in your program.

    In comparing assembler, procedural, and object oriented a procedural language is much much closer in its structure to object oriented than it is to assembler. All high level language constructs share the fact that one statement in the source almost always equates to many statements in the machine code (possibly many thousands) whereas with assembler that relationship is always one to one.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •