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


<h1>Intro</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est neque, feugiat sit amet malesuada ut, mollis nec arcu. Cras eu sem vitae urna mattis ultrices. Curabitur pellentesque, ipsum in feugiat faucibus, felis purus faucibus urna, a semper enim dui et lectus. Integer porta eros id turpis volutpat pulvinar. Ut posuere condimentum lacus in bibendum. Duis in massa ut lorem semper euismod sed in elit. Nunc in sem at nibh porta lacinia vel ut sem. Sed eget egestas dolor. Aliquam metus neque, adipiscing vitae cursus sed</p>

about.php


<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">'."\
"; 
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>'."\
";
} 
}
echo '</ul>'."\
";
?>

gallery.php


<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">'."\
"; 
  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>'."\
";
} 
echo '</ul>'."\
";
?>

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

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.

That would be bad practice.

That would be bad practice.

Can I ask why please?

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?

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.