SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Converting a small website to a single class? Good or bad practice?

    Hello All,

    I've written a very small website. Lets say 3 pages, some static pages - some with DB calls:

    intro.php
    PHP Code:
    <h1>Intro</h1>
    <
    p>Lorem ipsum dolor sit ametconsectetur adipiscing elitIn est nequefeugiat sit amet malesuada utmollis nec arcuCras eu sem vitae urna mattis ultricesCurabitur pellentesqueipsum in feugiat faucibusfelis purus faucibus urnaa semper enim dui et lectusInteger porta eros id turpis volutpat pulvinarUt posuere condimentum lacus in bibendumDuis in massa ut lorem semper euismod sed in elitNunc in sem at nibh porta lacinia vel ut semSed eget egestas dolorAliquam metus nequeadipiscing vitae cursus sed</p
    about.php
    PHP Code:
    <h2>What we do?</h2>
    <p>Fusce sed quam sit amet nibh viverra dictum ut quis tellus. Sed dignissim congue libero sed accumsan. Cras id nisi eget enim gravida pellentesque. Sed nisl tortor, blandit eu pretium sed, rhoncus ut orci. Phasellus semper, orci eget sodales rutrum.</p>;
    <?php
    $sites 
    = array(1,2,3,4,5,6,7,8,9);
    shuffle($sites);
    $count 1;
    echo 
    '<ul id="portfolio">'."\n"
    foreach (
    $sites as $site) { 
      
    $QUERY $dbc->query("SELECT * from site WHERE site_id = $site");  
      
    $QUERY->setFetchMode(PDO::FETCH_ASSOC);  
      while(
    $ROW $QUERY->fetch()) {  
        echo 
    '<li>'.$ROW['name'].'</li>'."\n";

    }
    echo 
    '</ul>'."\n";
    ?>
    gallery.php
    PHP Code:
    <h2>Online Gallery</h2>
    <p class="bodytext">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus feugiat urna est, vel molestie turpis. Praesent iaculis ultricies diam quis elementum. Duis lorem elit, eleifend in lobortis in, tincidunt semper neque. Etiam viverra scelerisque felis, non rhoncus urna euismod eu</p>
    <?php
    $QUERY 
    $dbc->query("SELECT * from gallery ORDER BY Rand()");  
    $QUERY->setFetchMode(PDO::FETCH_ASSOC);  
    echo 
    '<ul id="portfolio">'."\n"
      while(
    $ROW $QUERY->fetch()) {  
        echo 
    '<li><img src="'.SITE.'images/bmg2/gallery_small/'.$ROW['image'].'" alt="'.$ROW['about'].'" /><a style="cursor: pointer;" onclick="window.open(\''.URL.''.$ROW['id'].'/viewpicture\',\'popup\',\'width=600,height=400,scrollbars=yes,resizable=yes,left=50,top=0\'); return false "><span>Enlarge Image</span></a></li>'."\n";

    echo 
    '</ul>'."\n";
    ?>
    Now as you can see they all sit on different pages ie:

    - intro.php
    - about.php
    - gallery.php

    and do different things. But what i'm thinking is simply putting them all in a class called something like Website so they are on one single page rather than individual pages to make the structure cleaner.

    Question is though, is this bad practice? Am I using OO properly here or am I just chucking a load of functions into a class (which some people do and others frown upon this) to make things easier for myself?

    How would you guys handle a small project like this? Is it wrong to just set up a single class called Website for something like this? Any feedback welcome

    Thanks

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,832
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by chris_stfc View Post
    How would you guys handle a small project like this? Is it wrong to just set up a single class called Website for something like this? Any feedback welcome
    Thanks
    These are currently separate pages and you want to keep them as separate pages or put everything all on one page?

    If you want separate pages, you will either need to rewrite the URL using mod_rewrite and them create a short bit of code to determine which resource is being requested then display that content. Or you will need to create stub files which will then load the appropriate script.

    What I started doing is the basic model-view-controller paradigm of sorts. I have two classes, one for the logic and one to display the content, or a template class. The controller part of my script is so short I didn't put it in a class. I also have a separate class for all database accessing.

    The request comes in. If it is rewritten, it passes through the controller code to determine which resource is being requested. Then it loads a logic class and then calls the appropriate logic function. Then the logic class loads the template class which outputs the HTML.

  3. #3
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris_stfc View Post

    Question is though, is this bad practice? Am I using OO properly here or am I just chucking a load of functions into a class (which some people do and others frown upon this) to make things easier for myself?

    How would you guys handle a small project like this? Is it wrong to just set up a single class called Website for something like this? Any feedback welcome
    That would be bad practice.
    aaron-fisher.com - PHP articles and more

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That would be bad practice.
    Can I ask why please?

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You are just chucking a bunch of functions into a class. Why? What would be the point in that? Just doing it because it is the current buzz word?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris_stfc View Post
    Can I ask why please?
    It misses the point of OOP entirely. OOP in php is largely about identifying and defining the different aspects of your website (of which there are many) into reusable objects, separating responsibilities, and making future development changes easier. Having one giant 'website' class does not accomplish any of those goals.
    aaron-fisher.com - PHP articles and more


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
  •