Jump to content

Practical PHP - Simple OOP site wont render in browser


fersick

Recommended Posts

I have been working through the text book. Practical PHP by Paul Hudson.

 

The chapter on creating a simple OOP site. wont render in a browser. I have written the code out get not errors in the debugging console i see my rendered HTML code in the console. But in a browser i get code 

 

setPage($page); $content = <<setContent($content); $site->render(); ?>

 

 

 

 

These are the files i have written

 

index.php

<?php
include 'stdlib.php';

error_reporting(E_ALL);
  ini_set('display_errors', 1);

$site = new csite();

// This is a function specific to this site!
initialise_site($site);

$page = new cpage("Welcome to my site!");
$site->setPage($page);

$content = <<<EOT

Welcome to my personal web site! 
EOT;

$page->setContent($content);

$site->render();
?>

 I feel it has something to do with the EOT reference, as the code surrounding the EOT is what is outputted in browser.

 

 

 

stlib.php

<?php
function __autoload($class) {
	include "$class.php";
}

function initialise_site(csite $site) {
	$site -> addHeader("header.php");
	$site -> addFooter("footer.php");
}
?>

 

 

 

csite.php

 

<?php
class csite {
	private $headers;
	private $footers;
	private $page;

	public function __construct() {
		$this -> headers = array();
		$this -> footers = array();
	}

	public function __destruct() {
		// clean up here.
	}

	public function render() {
		foreach ($this->headers as $header) {
			include $header;
		}

		$this->page->render();

		foreach ($this->footers as $footer) {
			include $footer;
		}
	}

	public function addHeader($file) {
		$this -> headers[] = $file;
	}

	public function addFooter($file) {
		$this -> footers[] = $file;
	}

	public function setPage(cpage $page) {
		$this -> page = $page;
	}

}
?>

 

 

cpage.php

 

<?php
class cpage {
	private $title;
	private $constant;

	public function __construct($title) {
		$this -> title = $title;
	}

	public function __destruct() {
		// clean up here
	}

	public function render() {
		echo "<H1>{$this->title}</H1>";
		echo $this -> content;
	}

	public function setContent($content) {
		$this -> content = $content;
	}

}
?>

 

 

header.php and footer.php are just basic HTML code.

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.