Jump to content

Help with Classes


LejeuneBay

Recommended Posts

I have 2 classes, a template class and a bumper class (the bumper class redirects a user away from a page which is useless to the user but vital to internal server functions to add, edit, or delete from MySQL). Basically what problem I'm having is simplifying my programing code from 7 lines to 1 line.

 

The major problem resides with trying to call "globals" and a list of given variables to replace on a template.

 

For instance, when I put down

 

(Index.php)

$VARS = "\$SITE_TITLE, \$SITE_DESCRIPTION";

template("templates/default/index.tpl"…

 

The website appears just fine with the {SITE_TITLE} and {SITE_DESCRIPTION} replaced on the template.

 

But when I try to implement classes it doesn't replace any of the {VARIABLES} that are listed. Here is the code:

 

class template{


function display($file, $vars, $x) {

	if($x == 1 || $x == 2){

		$line	=	@implode(file($file),"");

	}else{

		$line	=	file_get_contents($file);
	}
	eval("global $vars;");
	$vars = str_replace(", ","",$vars);
	$vars_list = explode("\$",$vars);
	for($i =0; $i <= sizeof($vars_list); $i++) {    
		$line = str_replace("{".$vars_list[$i]."}",$$vars_list[$i],$line);
	}
	if($x == 1){
		print $line;
	}elseif($x == 2 || $x == 3){
		return $line;
	}
}

}

class bumper extends template{

var $bumperTemplate;
//var $vars;

// Constructor
function bumper($bumperTemplate){

	/*
	 * Bumper Logic Here
	 * 
	 */
	$this->bumperTemplate 	= $bumperTemplate;
	//$this->vars				= $vars;

}

function redirect($url, $message, $delay, $classId){

	$PHP_SELF		=		basename($_SERVER['PHP_SELF']);

	$MESSAGE 		=		"<span id=\"".$classId."\">".$message."</span>
							<br><br>Redirecting...<br>
							If the page doesn't redirect automatically <a href=\"".$url."\">Click here</a>.";

	$LOCATION 		=		$url;
	$DURATION 		= 		$delay;
	$vars = "\$MESSAGE, \$LOCATION, \$DURATION, \$PHP_SELF";
	parent::display($this->bumperTemplate,$vars,1);

}

}

$template = new template();
$bumper = new bumper();

$bumper->redirect("index.php","You have successfully logged into your account.",3,"BumperCorrect");

 

 

The code should be self explanatory. Basically that "display" function under the template class is not getting the following variable values: $PHP_SELF, $MESSAGE, $LOCATION, $DURATION which are listed correct for the display function in the $vars variable.

 

The display function outputs the file, and erases all the {VAR_NAME_HERE} type variables located on the template.

 

Does any one have any suggestions? Or need me to explain it in a different way?

Link to comment
https://forums.phpfreaks.com/topic/218684-help-with-classes/
Share on other sites

This may or may not be the issue.  But it would certainly make it easier to follow what you're doing.  Try passing your list of variables as an array:

 

$vars['MESSAGE'] = "This is my message";

$vars['LOCATION']= $URL;

$vars['DURATION']=$duration;

$vars['PHP_SELF']=$php_self;

 

Then you don't have to worry about trying to explode the string out into an array.

Link to comment
https://forums.phpfreaks.com/topic/218684-help-with-classes/#findComment-1134263
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.