Jump to content

Inheritance Blues


ksmatthews

Recommended Posts

Hi Gurus,

 

I have a simple class below called 'Page'.

Below this I am trying to inherit from Page and overide a parent method called addHeader().

 

When I try to use the child class (at the very bottom) I get nothing ! Why ?

 

// Page class - creates web pages on the fly

class Page

{

  // Declare class member variable

  var $page = "";

  var $title = "";

  var $copyright = "";

  var $creator = "";

  var $message = "";

 

  // The base constructor

  function Page($title, $copyright, $creator, $message)

  {

    // Assign values to member variables

    $this->page = '';

    $this->title = $title;

    $this->copyright = $copyright;

    $this->creator = $creator;

    $this->message = $message;

  }

 

  // Generates the top of the page

  function addHeader()

  {

  $this->page .=

'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">';

 

more HTML here ...

  }

 

// Gets the contents of the page

  function get()

  {

    return $this->page;

  }

 

} // end of class Page

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

class newpage extends Page

{

var $page = "";

 

// constructor 

function __construct($title, $copyright, $creator, $message)

    {

      $this->page = '';

      parent::__construct($title, $copyright, $creator, $message);

    }

 

// OVERIDES the parent function

function addHeader()

  {

  $this->page .=

 

'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">';

       

  DIFFERENT HTML GOES HERE

 

  }

 

}

 

+++++++++++++ USE THE CHILD CLASS ++++++++++++++++++++++++++++++

// Instantiate Page class (inc optional DB update message)

$webPage = new newpage($pagetitle, CREATOR, COPYRIGHT, "");

 

// add header

$webPage->addHeader();

 

// Display the page so far

echo $webPage->get();

 

NOTHING ...

 

regards,

 

Steven M

 

Link to comment
Share on other sites

It's better to keep to the conventions of writing in PHP 5 as thorpe said. There is no need to declare $page twice, as it is inherited by the base class. Instead of var user public/privete/protected access descriptors (this is not an error but is outdated). Try to put some output in the get function and check what happens when it is called.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.