Avatar of MMDeveloper
MMDeveloper
Flag for United States of America asked on

PHP Redeclare Class Issue

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");
		}
	}

Open in new window

PHP

Avatar of undefined
Last Comment
Richard Davis

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Roger Baklund

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
MMDeveloper

ASKER
awesome, thanks
Richard Davis

Just a stab in the dark here because my better judgement tells me that those two classes are contained in two seperate files. If that is the case, then it is highly likely that you're...

include_once($this->shared->path . "/templates/" . $template . ".php");

...line is somehow calling the same file twice.

This would seem the most logical reason why you're receiving this error.

Additionally, your submitRegistration() function is calling the getTemplateResponse() function, which is declaring an instance of the class...immediately followed by sendMail() function which is also calling another instance. Perhaps the two calls are the source of this double declaration.


~A~
Richard Davis

Ooops...answered already...nevermind
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23