Hi All,

Can anybody see my error with this following class?

im expecting it to show a blank page, but with a title, and when i view source to show the page structure.

<?php
class webpage {
	private $doctype = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">";
	private $head    = "<html><head>";
	private $title   = "</head><title>success";
	private $body    = " </title><body>";
	private $ender     = "</body></html>";
	
	private function __construct(){
		$this->doctype = $doctype;
		$this->head = $head;
		$this->title = $title;
		$this->body = $body;
		$this->ender = $end;
		}
		
		public function content(){		
			
			$page = $this->doctype;
			$page .= $this->head;
			$page .= $this->title;
			$page .= $this->body;
			$page .= $this->ender;
			return $page;
		}
	
}

//class indexPage extends webpage{
//	
//}

$thing = new webpage();
echo $thing->content();

?>
Member Avatar for rajarajan2017

title should come within head tag

Hi,

Ive done it on purpose this way, so i can concatenate content

$page should hold the string of the page structure

sorry my mistake i see what you mean. This has made no difference though.

there seems to be something wrong with the construct. if i remove it, it works fine

Member Avatar for rajarajan2017

The below code is what you want, should write as:

<?php
class webpage {
	private $doctype;
	private $head;
	private $title;
	private $body;
	private $ender;
	
	
		public function __construct() {
			$this->doctype = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">";
			$this->head = "<html><head>";
			$this->title = "<title>success";
			$this->body = " </title></head><body>";
			$this->ender = "</body></html>";
		}
		
		public function content(){		
			
			$page = $this->doctype;
			$page .= $this->head;
			$page .= $this->title;
			$page .= $this->body;
			$page .= $this->ender;
			return $page;
		}
	
}

$thing = new webpage;
echo $thing->content();


?>

Thanks for this it worked fine.

I hope you dont mind a related sub question.

if added an extended class, which id like to concatenate to the $body.

i hope to have a class extension for each page, which when selected will concatenate to the $body

class index extends webpage{
	
	private $content;
			
		function __construct($doctype, $head, $title, $body, $ender, $content){
		
		parent::__construct($doctype, $head, $title, $body, $ender);
		$this->content = "i am the index page";
		
		}
		
		function indexcontent(){
			
		}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.