troubleshooting Question

PHP Redeclare Class Issue

Avatar of MMDeveloper
MMDeveloperFlag for United States of America asked on
PHP
4 Comments1 Solution1493 ViewsLast Modified:
I'm writing a "Lite" project management application in PHP and a lot of the functionality is template based (email templates for example).

I'm borrowing functionality ideas from Joomla 1.x so all my templates are OOP (example below). All templates use the same class name (html_template) and they all have the same method renderTemplate() which renders the HTML result and returns it. The method that I have that "renders the template result" is also below. The system works fine until I hit my Registration system. When it sends out the account activation email, it uses one template for the email, and every email automatically includes a "footer" template. Since the system "includes" 2 templates (both with the same class name) I get the "Cannot redefine class" error. How can I resolve this?

Essentially.. how can I have an OOP system that uses templates, each template using a common programming format, with the ability to either redefine the class, or destroy the class after using it so I can include another file with the same class name?
Template File Examples:
 
<?php
class html_template {
	public $vars = array();
 
	public function renderTemplate() {
		$template = '<br /><br /><br />---------------------------------------------------------<br />
				This email was generated from the MM Lite Project Management application.<br />
				This email is provided for informational purposes only. Please do not respond to this email.';
 
		return $template;
	}
}
?>
 
<?php
class html_template {
	public $vars = array();
 
	public function renderTemplate() {
		$template = '<b>Thank you for registering!</b>
				<br/ >
				<br />
				Username: ' . $this->vars["un"] . '<br/>
				Password: ' . $this->vars["pw"] . '<br />
				<a href="' . $this->vars["link"] . '">Click to Activate Account!</a>';
 
		return $template;
	}
}
?>
 
method that calls template, and email method
 
	public function getTemplateResponse($template = "", $data = array()) {
		$template = trim($template);
 
		if ($template != "") {
			include_once($this->shared->path . "/templates/" . $template . ".php");
			$template = new html_template();
			if ($template->vars) {
				$template->vars = $data;
			} else {}
 
			$value = $template->renderTemplate();
			unset($template);
			return $value;
		}
		else {
			return false;
		}
	}
 
	public function sendMail($to, $subject, $message, $headers = array()) {
		$messageHeaders = array();
 
		$defaultHeaders = array(
				"MIME-Version:" => "1.0",
				"Content-type:" => "text/html;charset=iso-8859-1",
				"From:" => "MM Lite Project Management Mailer Daemon <noreply@mechanicmatt.com>"
			);
 
		foreach ($headers as $k => $v) {
			$defaultHeaders[$k] = $v;
		}
 
		foreach ($defaultHeaders as $k => $v) {
			$messageHeaders[] = $k . " " . $v;
		}
 
		$footer = $this->getTemplateResponse("mail/footer");
 
		return mail($to, $subject, $message . $footer, implode("\r\n", $messageHeaders) . "\r\n");
	}
 
 
 
The method that starts the process
	private function submitRegistration() {
		$data = $this->escapeArray($this->shared->request);
		$password = $this->generatePassword();
 
		$response = $this->database->query("INSERT INTO todo_users (email, name, username, password, cellphone, enabled, isCreator) VALUES('" . $data["userEml"] . "','" . $data["name"] . "','" . $data["userName"] . "',PASSWORD('" . $password . "'),'" . $data["userCell"] . "','0','" . $data["role"] . "')");
		$userID = $this->database->insertid();
		if ($response !== false) {
			$token = md5(uniqid(rand(), true) . serialize($_REQUEST));
			$this->database->query("INSERT INTO todo_tokens (token, userID, expiry) VALUES('" . $token . "','" . $userID . "',DATE_ADD(NOW(), INTERVAL 1 DAY))");
			$templateVars = array (
					"un" => $this->shared->request["userName"],
					"pw" => $password,
					"link" => $this->shared->absolute . "index.php?component=registration&task=activate&token=" . $token . "&uid=" . $userID
				);
			$message = $this->getTemplateResponse("mail/sendActivationLink", $templateVars);
 
			$this->sendMail($this->shared->request["userEml"], "Account Activation", $message);
 
 
			header("Location: " . $this->shared->absolute . "?component=registration&task=afterReg");
			die();
		}
		else {
			$this->highlightError($this->errors->registrationInsertError, "high", "?component=registration");
		}
	}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros