1

I try to do class tree where each class in tree check own directory for template and use it, but when I call function in inherited class then called parent. How can i do it ?

My code in example below output:

D
C
b/1.phtml

but i need to d/1.phtml

<?php

class A {
    private $templates_dir = 'a';
}

class B extends A {

    private $templates_dir = 'b';

    public function templates_dir()
    {
        return $this->templates_dir;
    }

    public function check_template($tpl)
    {
        $dir = $this->templates_dir();
        $file = $dir. '/'. $tpl;
        echo (get_class($this)). "\r\n";
        echo (get_parent_class($this)). "\r\n";
        echo $file . "\r\n";
// idea  - if (!file_exists($file)) return parent::check_template($file);
// method call each class while template will be found
// how do it?


    }

}

class C extends B {

    private $templates_dir = 'c';

}

class D extends C {

    private $templates_dir = 'd';

}

$obj = new D();
$obj->check_template('1.phtml');
1
  • With all those subclasses you're entering in a world of pain. Commented Jun 4, 2017 at 14:04

2 Answers 2

1

I would just make $templates_dir protected:

class A {
   protected $templates_dir = 'a';
}

and adjust the extending classes to do the same.

That will then cause templates_dir() to return whatever $templates_dir is set to.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it's solution for templates_dir, but the main idea is redefine template and without redefine function check_template in each child class calling parent:check_template call first parent class where is defined.
I want each class in class tree find template in own templates dir and if there is not template call immediate parent class for check this template in own templates_dir and further until the template is found
1

Another approach will be placing the functions in a abstract class, and each of class A,B,C,D extends this, This is a neater way of doing things.

Below is the code --

    abstract class WW {

    protected function templates_dir()
    {
        return $this->templates_dir;
    }

    public function check_template($tpl)
    {
        $dir = $this->templates_dir();
        $file = $dir. '/'. $tpl;
        echo (get_class($this)). "\r\n";
        echo (get_parent_class($this)). "\r\n";
        echo $file . "\r\n";
    // idea  - if (!file_exists($file)) return parent::check_template($file);
    // method call each class while template will be found
    // how do it?


    }
}

class A extends WW {
    protected $templates_dir = 'a';
}

class B extends WW {

    protected $templates_dir = 'b';



}

class C extends WW {

    protected $templates_dir = 'c';

}

class D extends WW {

    protected $templates_dir = 'd';



}

$obj = new D();
$obj->check_template('1.phtml');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.