views:

543

answers:

5

I'm actually creating websites for fun and some of my friends told me that I could be more efficient if I could create the output of the page with a php class that would represent the whole page.

I was wondering how you people would do it.

Thanks

+1  A: 

I'd suggest looking into some PHP frameworks, such as Cake which is good for beginners, or Symfony or Zend Framework if you're more skilled. Those will streamline your PHP development a lot.

Jani Hartikainen
A: 

What you could do is have all the tags in the page stored as a multi-dimensional array. PHP is mushy enough to do this. As such, every tag would be stored in an element of the array. Then, you can use control statements to print different parts of the page at different times depending on certain conditions. It would kinda look like what you get when you convert an XML file to an array. Here's what I'm thinking.

$page['html']['title'] = "Page's Title";
$page['body']['p'][1] = "1st Paragraph";
$page['body']['p'][2] = "2nd Paragraph";

And then you could use your own functions to print these out as HTML fields.

chustar
A: 

I would suggest using some of the MVC (Model, View, Controller) frameworks out there like KohanaPHP. It is essentially this. You're working in a strictly Object-Oriented environment. A simple page in Kohana, build entirely from a class would look like this:

class Home_Controller extends Controller
{
  public function index()
  {
    echo "Hello World";
  }
}

You would then access that page by visiting youur url, the class name, and the method name:

http://www.mysite.com/home/ (index() can be called after home/, but it's implicit)

When you start wanting to bring in database-activity, you'll start working with another Class called a Model. This will contain methods to interact with your database, like the following:

class Users_Model extends Model
{
  public function count_users()
  {
    return $this->db->count_records('users');
  }
}

Note here that I didn't write my own query. Kohana comes with an intuitive Query Builder.

That method would be called from within your Controller, the first class that we mentioned at the beginning of this solution. That would look like this:

class Home_Controller extends Controller
{
  public function index()
  {
    $usersModel = new Users_Model;
    $userCount = $usersModel->count_users();

    echo "We have " . $userCount . " users!";
  }
}

Eventually, you'll want more complicated layouts, which will involve HTML/CSS/Javascript. At this point, you would introduce the "Views," which are just presentation layers. Rather than calling echo or print from within the Controller, you would load up a view (an HTML page, essentially) and pass it some variables:

class Home_Controller extends Controller
{
  public function index()
  {
    $myView = new View("index");
    $usersModel = new Users_Model;
    $userCount = $usersModel->count_users();

    $myView->userCount = $userCount;
    $myView->render(TRUE);
  }
}

Which would load the following "View"

<p>We have <?php print $userCount; ?> users!</p>

That should be enough to get you started. Using the MVC-style is really clean, and very fun to work with.

Jonathan Sampson
A: 

I am not a OO programmer .. but, as a proof of concept, and as per your question, you can do/try something like this.

class Page {
    public $meta_title;
    public $meta_keywords;
    public $html_body;

    public function displayPage() {
     $page ='
     <html>
     <head>
      <title>'.$this->meta_title.'</title>
      <meta name="keywords" content="'.$this->meta_keywords.'" />
     </head>
     <body>
      '.$this->html_body.'
     </body>
     </html>
';
     echo $page;
    }
}

Then you just use this Page class as ..

$page = new Page();
$page->meta_title ="Hello world!";
$page->meta_keywords = "some text keywords";
$page->body = '<h1>Contact Us </h1>
<p>you can contact us at blah ... blah .. etc.</p>
<address>Dummy Address </address>
';

$page->displayPage();

Please note that you can add so many things into to it like .. class variables (type array) to define stylesheets, javascripts files .. then you just loop over it to define these files dynamically for individual pages.

You can amend the display page function so that it accomodate left, right, or top navigation bar. Then you can also have variables like $show_right_bar, $show_left_bar to control which pages display which side navigation bar. So you can amend and extend to whatever your requirements are.

Alternatively, you can try some php frameworks, which are much more evolved solutions, but that really depends on your programming skills and your requirements.

Hope this helps.

Wbdvlpr
A: 

So many ways to do this, indeed.

When I make a simple personal site, I typically have a banner at the top and a menu on one side. To prevent myself from having to copy'n'paste everything, my index.php checks a get variable, and based on that get variable loads the appropriate content in the middle. By way of example, my index.php. My menu actually loads the content with a function, but this is not necessary/recommended:

<?php

$page = strtolower($_GET['page']);
if(!isset($page) || $page === '')
{
    $page="home";
}

switch($page)
{
    case "loomis":
     require_once("loomis.php");
     break;
    case "home":
    default:
     $page = "home";
     require_once("home.php");
     break;
}

include "menu.php";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Nebulous - <?php getTitle($page) ?></TITLE>
<link href="style.css" rel="stylesheet" type="text/css" />
</HEAD>

<body>
<?php displayMenu() ?>
</body>
</html>

Classes are really only useful as representations of data or functionality, not really to render pages.