PHP Class musings

Discussion in 'Web Design and Development' started by Cabbit, Apr 12, 2009.

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    As part of my plans for expanding my programing knowledge i have decided to try out making Classes since i have mastered making usable and very handy functions.

    I very much hope this scenario is possible so i can explore the means to make it happen and it will result in hopefully much more secure database queries as they will all use the one method.

    So my question today is related to functionality that i would like to know if it is possible to successfully execute my scenario.

    Scenario.

    Have a class that i can send it variable forum variables and have it add new records to a database table.

    form 1
    send title
    send body
    to tbl_post

    form 2
    send title
    send body
    send username
    to tbl_reply


    Class (send to database)
    get tbl name

    if title sent store title
    if body sent store body
    if username sent store username

    do validation

    upload validated data to tbl
    return result

    Result Page

    if result true
    successful entree
    else
    show errors
     
  2. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #2
    No problems so far. Not sure what you thought you can't do something with a class, it's just a simple collection of usually private functions and variables that may be inter-related, plus the class itself may be extended and even the functions overridden by other functions. Some important reasons people code in classes is to re-use modular code to save effort and to share modular code between developers, or to develop a framework.

    Here is a great tutorial on simple class creation and how functions and variables used within the class are accessed, how the class object is initiated and data returned. All the stuff you mentioned.

    When you write the class, you can use two traditional approaches to passing and returning date. Pass a myriad of arguments to the constructor, or use set/get functions to do the same. Both are OOP common principles, nothing specific just to PHP.

    -jim
     
  3. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #3
    Thanks i have started writing my class out now and am getting use to the way classes work.
    So far i have written a tempting engine that cashes the page that was dreadfully simple and am currently working on my database one for adding new items to the database.
    To me compared to my old manual methods it take longer to write the code the first time but the value of putting in something like.
    PHP:
    // Posts the record to the database
    $post = new newrecord;

            
    // db_tbl
            
    $post->db_tbl "forum-posts";

        
    // Send variables
         
    $post->subject $_POST['subject'];
        
    $post->body $_POST['body'];

        
    // Display output
         
    $post->ResponseFromDatabase();
    In the long run that is a lot easier to code than manually righting out and validating sql as all the validation functions are now stored in the class. And my new template engine though simple in nature just works a lot better than my old methods sign me up for a lover for classes and they were beautifully easy to learn or perhaps my coding skills are just evolving faster than i originally anticipated.
    Hmm some of that grammar and spelling would be nice i would love to learn how to produce a solid sentence that does not look like a 5 year old wrote it >.<
     
  4. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #4
    You came across fine and you are coding exactly the way I would expect a class to be used. To expand on your example, in production web sites there are connection classes to simplify database connectivity across many database formats, email classes (to handle sendmail, smtp, file attachments, etc) and error classes (for debugging and error screens) and so on. Traditionally these classes are all saved as individual PHP files to easily compartmentalize the code then include them into your code where applicable and simply initiate the object to start working with the functions. This process is very efficient and the core of the open source initiative's emphasis on sharing portable code.

    There are many public domain classes at phpclasses.org which is where I go all the time to find the best classes. You also might want to visit there simply to see how others do it, and to spark your creative energies.

    As you know I am an advocate of OOP programming, and wish more of us would post example code that use classes in the PHP world like the ones I listed in the first paragraph -- intended for the larger or more complex projects where efficient code is key.

    -jim
     
  5. Mr.nix macrumors member

    Joined:
    Jan 30, 2007
    #5
    You should take a look at the Zend Framework. I think you would be very surprised how easy all this would be. I've noticed people who are new to ZF have the most problem with just setting up a skeleton app. I wouldn't mind helping you out if you would like to use it.
     
  6. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #6
    I gave the Zend framework a try, i followed the tutorial but like with the ruby on rails one i also followed it just did not work at all.
    Would love to learn one of these frameworks just for the knowledge of how to work them.

    SrWebDeveloper thanks for the link and i will be sure to post any interesting classes i develop. For now my classes are far to immature though they are preforming there functions and i am most happy with my tempting system as it allows the page layout and design to be done in html which can be validated. And i keep all html separate from PHP this way.
     
  7. &Ingonyama macrumors member

    Joined:
    May 6, 2008
    #7
    Have you tried Codeigniter? It's a lot easier to set up than Zend and has good documentation, out of all the frameworks I think its the best one to get started with especially if you already have experience with PHP.
     
  8. Mr.nix macrumors member

    Joined:
    Jan 30, 2007
    #8
    I really don't think the docs are setup very well for a skeleton. I can zip one up that works right away.
     
  9. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #9
    You need a very recent version of PHP for the latest Zend framework to work (5.2.4 if I remember correctly). If you have an older version, or Apache is not enabled for URL rewriting (like the one that comes built into Mac OS X) then you will have problems. That is why I recommend people build Apache and PHP from source so they can enable all the features they want while still being able to disable the unneeded features.
     

Share This Page