I written a class for loading an template file and replacing the variables with actual data.
Please review my code and suggest me the best code.
My Code:
<?php
/**
* This class will helpful for variable replacement from a loaded file
*
* @author V V R
* @version 1.0
*/
class Parser {
public $dir = "";
public $text = "";
/**
* To get directory location
* @param $dir
*/
function __construct($dir = "home/template") {
$this->dir = $dir;
}
/**
* To load the specified file from directory
* @param $file
*/
function loadFile($file) {
try {
if (file_exists ( $this->dir . "/" . $file )) {
$this->text = file_get_contents ( $this->dir . "/" . $file );
} else {
throw new Exception ( $this->dir . "/" . $file . " does not exist" );
}
} catch ( Exception $e ) {
error_log ( $e->getMessage () );
die ();
}
}
/**
* To get the text from a file
* @return text
*/
function getText() {
return $this->text;
}
/**
* To replace the variables
* @param unknown_type $var
* @param unknown_type $text
*/
function replace($var, $text) {
$this->text = str_replace ( "{\$$var}", $text, $this->text );
}
}
?>