WEB Classes and Objects

Discussion in 'OT Technology' started by hurleyint1386, Oct 31, 2008.

  1. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    When doing PHP web development, when do you use objects? Up until now, I've done everything with out functions, methods, classes, objects or anything like that. I picked up a few books that use classes and objects, however it doesn't say when to use them. I've never needed to use them, however I'm sure there's a way to make things easier when using objects, but I just don't know when. So tell me, when do you decide to use a class and an object?
     
  2. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,915
    Likes Received:
    8
    Location:
    Los Angeles
    You actually don't ever NEED to use them. Object oriented programming is a convenience. OOP makes web applications easier to maintain by yourself and other people. OOP also allows you to rapidly develop your web applications because most of the classes and objects you are using should be reusable code that you can use on most projects.

    I suggest you learn the basics of object oriented programming, then move onto frameworks, where they'll be providing you most of the classes that you can extend with your own.
     
  3. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    I've done object-oriented programming, although it seems different with PHP. I think it's because I've been doing, I guess you can call it, spaghetti code for a while now, I'm just used to it. When doing programming, I understand having objects and having certain properties for those objects, using inheritance for more specific properties, but I just don't see when you would really need to use an object with php.

    I just figure if someone was hiring a developer for websites, then they would almost definitely need to use objects.
     
  4. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,915
    Likes Received:
    8
    Location:
    Los Angeles
    PHP is a loosely typed language so object oriented programming is going to be different from Java or C++ programming. But it's still object oriented programming.

    A lot of people still write their code using procedural methods. While that's fine and all, when you need to revisit the project, or when you need to give it to another developer, it's going to be less maintainable than a project that was developed using OOP methods. OOP also provides division between your structure and presentation. By that, I mean, it keeps your programming code separate from your HTML/CSS, like it should be. Mixing PHP with HTML and you've got what you described as "spaghetti code". Everything blended together, mashed up, and a nightmare to maintain and upgrade.

    Not too mention it saves an incredible amount of time.
     
  5. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    I think I'm going to start using more structure when doing development for the future because right now, I'm not doing things very well. I have everything working great, and I know the old saying "If it ain't broke, don't fix it" although if I do need to update it in the future, it could break very easily.
    Right now, I do mix php with html, although how do most people do it? Do they have html pages and just include PHP pages? Sorry, I'm new to this. I am trying to change my ways though! haha

    I do include mysql connect statements in a private directory for my host, so I am safe about that. I don't feel right having my raw password in a public file. So I include a file with the specific connect statements.
     
    Last edited: Oct 31, 2008
  6. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,915
    Likes Received:
    8
    Location:
    Los Angeles
    There's a difference between what "most people do" and the "correct way" of doing things. One could always argue that their way is correct by doing things, but sometimes it isn't the right way. If it's just a simple four page site that you can hack out in less than a day, arguably, a lot of people would think it's overkill to plug a site like that into a framework or templating engine of some sort. I'd agree to an extent. To answer your question, a lot of people use templating engines, such as Smarty, PHPTemplate, PHPTAL, Savant, etc. I'd say the most notable in the PHP community is Smarty. But I like PHPTemplate (With Drupal). Using a templating engine will keep your HTML clear from code. The templating is basically used as an output generator from your code to your HTML. Usually server side includes are minimal while using a templating engine as the engine usually handles most of that.
     
  7. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    loosely typed..like JS... :squint:
     
  8. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,915
    Likes Received:
    8
    Location:
    Los Angeles
    biawokauns can code it in 2 mins tops.
     
  9. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    Instead of asking this question on an internet forum where you will get questionable knowledge back at you, educate yourself with professional books and articles on the theory of OO.

    It's just the tip of the ice berg really. OO is just another tool, and it enables you to use a lot of the current industry approaches for making development better.

    As a general rule of thumb, I would use a class over a function when the function grows to anything other than a trivial amount of code. I'd discourage mixing of functions and objects and try to do pure OO as much as possible, but this is difficult with PHP.

    As for mixing presentation (HTML) with code is a big no-no, and you need to read up on that too.
     
  10. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,488
    Likes Received:
    69
    Location:
    Sparkopolis
    questionable knowledge.. from kingtoad?

    :ugh2:
     
  11. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    When you are referring to mixing presentation with code, do you mean something like this would be unacceptable?

    Code:
    <div id="body">
            <? echo "This is not right<br />"; ?>
    </div>
    
    If so, what would be the correct way of doing it?

    Also, if you are creating a method within a class, then you would use the keyword 'function' for that. So above, I assume you mean an actual function rather than a method within a class.
     
  12. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    Yeah in PHP you would use the "function" keyword to define the method. So even though it's got the "function" keyword, it's technically a "method" by OO definition and should be called such to avoid confusion.

    A correct way of doing your example would be something like:

    logic.php
    Code:
    $message = "This is not right"
    $smarty->assign('message', $message);
    $smarty->display('mytemplate.tpl');
    
    mytemplate.tpl:
    Code:
    <div id="body">{$message}<br/></div>
    
    Now if you decide the message shouldn't be in a div, or you want that div to have a different id, or you want to drop the <br/> you just change that template without touching the program logic. You could even get your HTML coder to do that if they knew basic Smarty.
     
  13. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    Do a lot of people use Smarty? I've never heard of it. I guess I've got something to learn. Is there a place where I can find a template page for how it is used? I learn better by a larger scale example Thanks a lot for the information.
     
  14. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    Yeah Smarty's very popular. I used it in my PHP CMS and there's a lot of it I really like, and some annoyances as well. I'd definitely recommend it if you're using PHP.

    Interestingly, it looks like they're finally starting Smarty 3! Keep an eye on that as it's alpha, and just go with Smarty 2.

    They have good docs, whet your appetite with the crash course: http://www.smarty.net/crashcourse.php

    One thing that I would recommend is change the Smarty delimiters from curly braces to double curly braces, mainly if you do lots of javascript. This is because javascript is heavy on curly braces. This means you can go from this:
    Code:
    <style type="text/css">
    {literal}
    <!-- ^^ We have to use "Literal" tag so that Smarty doesn't confuse the
           curly braces in the CSS as Smarty tags -->
    .cssclass {
    }
    {/literal}
    </style>
    <script type="text/javascript">
    // It's the same deal with javascript
    {literal}
    function test()
    {
       // This gets messy if you want to use Smarty code in javascript as
       // it turns into template escaping madness
       alert('The template message variable is "{/literal}{$message}{literal}"');
    }
    {/literal}
    </script>
    
    Now if the delimiters are changed from { and } to {{ and }} you can do this:

    Code:
    <style type="text/css">
    .cssclass {
    } /* This will not be parsed as Smarty and won't generate errors */
    </style>
    <script type="text/javascript">
    function test()
    {
       alert('The template message variable is "{{$message}}"');
    }
    </script>
    
    Just be wary of doubling up braces in javascript like this:

    Code:
    var obj = {{nestedObjName: "test"}, anotherValue: "test2"};
               ^^ Smarty will think this is a Smarty tag, we need a space
                    between these braces.
    
    To change the delimiter you would do something like:

    Code:
    $smarty->left_delimiter = '{{';
    $smarty->right_delimiter = '}}';
    
    Or better yet, extend from the Smarty base class and in your class, do the setup so then when you fire things up all you need is:

    Code:
    $smarty = new MySmartyClass();
    
    Rather than

    Code:
    $smarty = new Smarty();
    $smarty->left_delimiter blah
    $smarty->blah blah
    $smarty->blahhh
    
     
  15. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    So just to clarify, a lot of PHP developers develop their sites like this? I'd really like to know because if I go apply for a developer position, I would like to have the best knowledge possible (naturally).
     
  16. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    Yes.

    Separating the presentation layer from the logic is just one of the first steps towards separating the concerns of the application into specialized chunks.

    The MVC pattern is an extremely popular approach used by a lot of frameworks and stand-alone apps that you might want to read up on.

    Also picking up and using a good PHP web application framework such as Zend Framework (which uses MVC) will help you learn and use good practices without having to write much stuff yourself.
     
  17. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    Awesome. Thanks a lot for all the input. I really appreciate it. I'm going to go play around with it.
     
  18. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    ya, at smaller businesses it might not be as big of a deal, but usually with large businesses they're separating the layers; that way they can have the coders working on the code, and the designers working on the design, and so on. instead of coders just making everything :mamoru:
     
  19. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    Just a question:

    When I'm connecting to a database, for each site, I have a non-public php page that does a connect to the mysql database. What I usually do is include the file in the site. Would this be fine? Or should I have some sort of connect class with a method for connecting to the database? My method works perfect, but I'd like to have an optimal solution.

    Also, I've been playing with Smarty and so far it's pretty cool on my home server. Although on my hosted server, I don't know how to install Smarty on the host. One of the procedures is to chown a couple folders, which I don't have the ability to do. Any ideas?
     
  20. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    Your method sounds ok for now. There's always things you can do to improve security, but you can overdo things and part of the approach to security is using measures within reason for your situation and the data you're protecting.

    I recommend reading the security section in the PHP manual http://www.php.net/manual/en/security.php and some of the articles on http://phpsec.org/

    I'm always spoiled having my own servers to play with, so maybe someone else can help you with the hosting problems. You're probably best to ask your hosting company directly how you can get some webserver-writeable directories set up.
     
  21. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    I actually got it all working. I was having trouble chowing the folders, but it actually didn't need to be. So it's all working now. My next site I'm going to try to do using Smarty. Thanks a lot.
     

Share This Page