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
https://forums.phpfreaks.com/topic/76207-inheritance-blues/
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
https://forums.phpfreaks.com/topic/76207-inheritance-blues/#findComment-385704
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.