Hi anyOne.
I studied in the php manual that
$this is available when a method is called from
within an object context (his class).
Well I found this simple class on phpclasses
the class:
<?php
/**
*
* Patrick J. Mizer
* <pmizer@mail.utexas.edu>
*
* May 28, 2006
*
* Very lightweight *but functional* template system.
*
*/
class SimpleTPL {
function SimpleTPL() {
}
function assignValue($name, $value)
{
$this->$name = $value;
}
function renderTemplate($templatePath)
{
if(file_exists($templatePath)){
require($templatePath);
}else {
$this->_handleError('Could not locate template: <b>' . $templatePath . '</b>');
}
}
function cleanUp()
{
@settype(&$this, 'null');
}
function _handleError($msg)
{
echo $msg;
}
}
?>
I tried this simple example:
//index.php
<?php
error_reporting(E_ALL);
require_once('SimpleTPL.class.php');
$tpl =& new SimpleTPL();
$name = 'Patrick J. Mizer';
$tpl->assignValue('name', $name);
$tpl->renderTemplate('header.tpl.php');
$tpl->cleanUp();
/* It works */
var_dump($tpl->name);
/* It doen't work */
var_dump($this->name);
?>
//header.tpl.php
<h1 id="">This is the Header</h1>
/* It doen't work */
<?php echo $tpl->name; ?>
/* It works */
<?php echo $this->name; ?>
In a nutshell I thought that the right
way to refer to the objects in the header.tpl.php
was $tpl->name but instead it works upside down
(in my opinion)
I don't understand this odd way to refer to
an object.
Could you explain me, please ? 🙂
Bye.