-1

I'm currently exploring a PHP project Latte which is templating system for PHP, and I've come across a piece of code that I'm having trouble understanding. Specifically, I'm interested in how dynamically created classes are typed within the project.

Here's the code in question:

public function createTemplate(string $name, array $params = []): Runtime\Template
{
    $class = $this->getTemplateClass($name);
    if (!class_exists($class, false)) {
        $this->loadTemplate($name);
    }
    $this->providers->fn = $this->functions;
    return new $class(
        $this,
        $params,
        $this->filters,
        $this->providers,
        $name
    );
}

In this code, we have a function called createTemplate which is supposed to create instances of dynamically generated classes. These classes are expected to extend a base class called Runtime\Template. The code specifies the return type as Runtime\Template, but I'm trying to understand how it ensures that the dynamically created class is of the correct type and how it actually invokes the constructor of the Template class.

I'm not encountering any issues; I'm simply looking for a clearer understanding of this aspect of the codebase.

Additional Information :-

  1. The shared code snippet is part of Latte project you can see the full code on https://github.com/nette/latte/blob/master/src/Latte/Engine.php
  • You should **at least** provide reference to the Latte manual what was followed in that code as you're hiding (with reason?) the implementation of both getTemplateClass() and loadTemplate(), both part of the protocol of $this, whichs' class you're hiding as well. – hakre Aug 20 '23 at 21:51
  • Yes thanks for suggestions, I added a link where you can see the actual class, the project is open source, so feel free to see whatever info you are looking for. – Harshit Singh Aug 21 '23 at 07:51

0 Answers0