Creating a Webpage using OO PHP - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Creating a Webpage using OO PHP

Jonathan1978Jonathan1978 Posts: 1Member
I am trying to get to grips with Object Oriented PHP. Last week we were given a exercise to do, which creates a webpage, using a class and a client which will call the class. I am totaly not getting the hang of this, and i am getting so frustraited with this. We are moving on to the next lesson this week, and i am worried i am going to fall behind, because i just cannot figure it out. Webpage.class.php holds the webpage class, where all the functions ect are held, and then i am supposed to use the client usewebpage.php to create a new webpage, but a week of trying, i have got nowhere, and i am hitting a brick wall trying to figure out how to do it.

Is there any OO php peeps out there who could give me a hand please? I am in a mess.

Thank You.


* This is a skeleton class for a Webpage class. We have given you the method names
* and a very brief comment on what those methods need to do. You will need to implement the class yourself.
* Also, included with this file is a file that is a 'client' or 'user' of the class.
* If you look at the way the client is using the class it'll give you some more clues about how
* you might need to implement the methods that the client calls.

* The class should be called: Webpage
* you will need attributes to hold at least the main sections of a page: the head, body and footer
Class Webpage {

private $head;
private $addToBody;

function __construct($head, $addToBody) {
// Store the doctype into the attribute of the class
$this->head = $head;
$this->addToBody = $addToBody;
//$this->styles = array() ??????;


function addToBody($toAdd){
$this->addToBody .= $toAdd;

function getPage(){
return $this->head . $this->addToBody;

/** Methods:
* A constructor
* The constructor for the class should accept at least two arguments: the title of the page, and an array of
* css filenames that it will use to create the appropriate code in the head section to link to those stylesheets
* The constructor should create the head section and footer section and give a default value for the body section
* addToBody
* a method called 'addToBody' that will add text to the body attribute of the webpage. See the client to see how this method
* will be used - it'll give you a clue as to how to implement it.
* getPage
* a getPage method which has as a return value the various sections, head, body and footer, of the webpage concatenated together.
* Consider carefully the scope of all attributes and methods/functions. Remember to make scope as restrictive
* as possible while still being consonant with the class working.



* @version $Id$
* @copyright 2008
// require the class definition file
require_once( 'webpage.class.php' );

// create a new instance of the webpage class passing the title and an array of stylesheets to the constructor
$page = new Webpage ("test", array( "dream.css", "main.css" ) );

// add new text to the page body
$page->addToBody( "

This is my first page constructed with my webpage class

" );
$page->addToBody( "

Look a the source html of this page and notice the doctype, the title and stylesheets are all correctly included and the code should validate as xhtml

$page->addToBody( "");

// echo the page contents back to the browser
echo $page->getPage();

Sign In or Register to comment.