Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

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
Avatar of Roger Baklund
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answers