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