ksmatthews Posted November 6, 2007 Share Posted November 6, 2007 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 More sharing options...
bache Posted November 6, 2007 Share Posted November 6, 2007 It works for me, are you using php 4 or 5? Link to comment https://forums.phpfreaks.com/topic/76207-inheritance-blues/#findComment-385683 Share on other sites More sharing options...
ksmatthews Posted November 6, 2007 Author Share Posted November 6, 2007 HI I am using php 5.0 Steven M Link to comment https://forums.phpfreaks.com/topic/76207-inheritance-blues/#findComment-385686 Share on other sites More sharing options...
trq Posted November 6, 2007 Share Posted November 6, 2007 You might try changing your Pgae classes constructor to.... function __construct($title, $copyright, $creator, $message) though I'm sure both should work. Link to comment https://forums.phpfreaks.com/topic/76207-inheritance-blues/#findComment-385688 Share on other sites More sharing options...
bache Posted November 6, 2007 Share Posted November 6, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.