Class does not extend from another

Asked

Viewed 236 times

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

  • 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 the global $tpl and I didn’t want this :/

2 answers

1


You are misimplemented your child class. I don’t understand the purpose of the template class, but for what you want to do this is enough:

class Spinxo extends Template {

    public function load() {
        $this->assign('web_title', 'titulo do site');
    }
} 

Spinxo already has the method assign inherited from Template. When using the class you also do not need to pass Template as a dependency on Spinxo:

$spx = new Spinxo();

$url = (isset($_GET['url'])) ? $_GET['url'] : 'home';

switch ($url) {
    case 'home':
        $spx->set('home');
        $spx->load();
        break;
}


echo $spx->show();
  • Um, why did I do it this way and had not returned the same result, now returned? I don’t know that solved my problem

  • 1

    I understood now, I had done so: $tpl->show and you passed $spx, correct thank you.

  • 1

    Look for more meaningful variable names @Guilhermespinxo. Helps you better view this type of problem :)

-2

Put a require_once in the code where you will give the extends

<?php

require_once("template.class.php");

class Spinxo extends Template {

    public function __construct() {
        $tpl = new Template;
    }

    public function load() {
        global $tpl;
        $tpl->assign('web_title', 'titulo do site');
    }
}
  • It is including the file, my problem is that I can only make the class work when I use global $tpl, I’d like to just use $this->assign() instead of $tpl->assign(), I won’t need to include, I’m already using __autoload();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.