SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    Iceland
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Somethings I don't understand about Classes

    I'm renewing my website soon. I read an introduction to classes by phpfreak and I liked the idea. The idea of keeping the code well structured and seperating markup from the scripts.

    I know I'm being very dense about this but I'd appreciate it if you'd clarify this for me.

    How is using classes different than simply including other php files with functions in your scripts?

    I have a php file named misc.php, for example. I keep it in /www/scripts/ and include it in the index file via "include('/scripts/misc.php');". Isn't that basically the same as having a class with the misc.php contents?

    I'm renewing my site completely pretty soon and I would like to be able to keep the XHTML and PHP relatively seperate, since it's currently pretty chaotic.

    How do I do this using classes? Since, really, I couldn't make it out from phpfreak's introduction.

    Let's say I have this piece of PHP code:
    PHP Code:
    function displaycomments($id) {
            global 
    $db;

            
    $query mysql_query("select id, rantid, date_format(postdate, '%M %D, %Y - %H:%m:%S') as date, name, comment from comments where rantid=$id");

            
    $todisplay "<table id=\"commenttable\" width=\"768\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
            <tr>
                    <td id=\"commenttitlecell\" width=\"768\" heigth=\"16\">
                            <strong>Comments</strong>
                    </td>
                    <br />
            </tr>
            <tr>
                    <td class=\"fill\" width=\"768\" heigth=\"16\">
                            &nbsp;
                    </td>
            </tr>\n"
    ;

            if(!
    mysql_num_rows($query)) {
                    
    $todisplay .= "<table id=\"nocomments\" width=\"256\" height=\"32\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
            <tr>
                    <td width=\"256\" height=\"32\">
                            No comments as of yet.
                    </td>
            </tr>
    </table\n"
    ;
            }

            while(
    $row mysql_fetch_assoc($query)) {
                    
    $todisplay .= " <tr>
                    <td class=\"commentheader\" width=\"768\" heigth=\"16\">
                            <p><strong>"
    $row['name'] ."</strong> - "$row['date'] ."</p>
                    </td>
            </tr>
            <tr>
                    <td class=\"comment\" width=\"768\">\n"
    ;

                    
    $comment nl2br(striptags($row['comment'], '<!---->'));

                    
    $todisplay .= "<p>"$comment ."</p>
                    &nbsp;
                    </td>
            </tr>
            <tr>
                    <td class=\"fill\" width=\"768\" heigth=\"16\">
                            &nbsp;
                    </td>
            </tr>\n"
    ;
            }
    ... 
    Would I gain anything from using classes to seperate the XHTML from the PHP? And how would I do it? If that's not asking too much...

    Thanks.

  2. #2
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need to create classes to seperate the XHTML from the PHP. Do that first and then you can actually see what your code looks like. Classes are used effectively in a different way than you misc.php file. Usually classes are created that each do a single testable thing, then combined for more complex operations. Keep learning about classes and OOP. As your application gets larger and more complex some of the benefits of OOP will become clear to you.


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
  •