-1
Hello I’m having trouble wearing extends
in a certain class
, see my code below:
Template class:
<?php
class Template {
private $_strings = array();
private $_template;
public function set($file) {
$path = 'templates/' . DEFAULT_THEME . '/' . $file . '.tpl.php';
if (!empty($path)) {
if (file_exists($path)) {
$this->_template = file_get_contents($path);
} else {
die("<b>Template error: </b> arquivo não encontrado: <i>{$file}.tpl.php</i>");
}
}
}
public function assign($string, $toReplace) {
if (!empty($string)) {
$this->_strings[$string] = $toReplace;
}
}
public function show() {
if (count($this->_strings) > 0) {
foreach ($this->_strings as $key => $value) {
$this->_template = str_replace('{' . $key . '}', $value, $this->_template);
}
}
return $this->_template;
}
}
Class using extends
class Spinxo extends Template {
public function __construct() {
$tpl = new Template;
}
public function load() {
global $tpl;
$tpl->assign('web_title', 'titulo do site');
}
}
What I want, I want that class Spinxo
load all settings, this is my home:
$tpl = new Template();
$spx = new Spinxo($tpl);
$url = (isset($_GET['url'])) ? $_GET['url'] : 'home';
switch ($url) {
case 'home':
$tpl->set('home');
$spx->load();
break;
}
echo $tpl->show();
As you can see I’m instantiating my object with the variable $tpl
when I take the extends
of my class Spinxo and use global $tpl
works... I don’t know if I could explain, but whoever understands please answer. : D
Why pass an instance of one of the parent class to the daughter class? I don’t understand what you want with this...
– gmsantos
It was just to try to explain, I wanted to use
$this->assign()
but my code only works when I use it$tpl->assign()
using theglobal $tpl
and I didn’t want this :/– Guilherme SpinXO