Home >Backend Development >PHP Tutorial >PHP framework - PHP tried to write its own template engine. The final display method is actually to require an HTML file. Why is it output?
Will the php require function output when it encounters text?
This is the template class I wrote:
<?php
/** * User: 火蜥蜴制作 */ namespace Core; // 模板类 class Template { private $data = []; private $path = ''; // 模板路径 public function __construct() { $this->path = Config::get('project.template_path'); } /** * 模板赋值 * @param $key * @param $value */ public function assign($key, $value) { if(is_array($key)) { $this->data = array_merge($this->data, $key); } else { $this->data[$key] = $value; } } /** * 获取路径 * @param $file */ private function getFilePath($file) { $params = explode('.', $file); // 模板路径已经加了分隔符 $path = ROOT . DIRECTORY_SEPARATOR . $this->path; foreach ($params as $key => $param) { if($key == count($params) - 1) { $path .= $param; } else { $path .= $param . DIRECTORY_SEPARATOR; } } return $path . '.html'; } public function display($file) { if(empty($file)) { throw new \Exception("Template Can Not Be Empty"); } $realPath = $this->getFilePath($file); if(is_file($realPath)) { extract($this->data); require($realPath); } else { throw new \Exception("Template:<code>{$realPath}
Not Found"); } } }Reply content:
Will the php require function output when it encounters text?
This is the template class I wrote:
<?php/** * User: 火蜥蜴制作 */ namespace Core; // 模板类 class Template { private $data = []; private $path = ''; // 模板路径 public function __construct() { $this->path = Config::get('project.template_path'); } /** * 模板赋值 * @param $key * @param $value */ public function assign($key, $value) { if(is_array($key)) { $this->data = array_merge($this->data, $key); } else { $this->data[$key] = $value; } } /** * 获取路径 * @param $file */ private function getFilePath($file) { $params = explode('.', $file); // 模板路径已经加了分隔符 $path = ROOT . DIRECTORY_SEPARATOR . $this->path; foreach ($params as $key => $param) { if($key == count($params) - 1) { $path .= $param; } else { $path .= $param . DIRECTORY_SEPARATOR; } } return $path . '.html'; } public function display($file) { if(empty($file)) { throw new \Exception("Template Can Not Be Empty"); } $realPath = $this->getFilePath($file); if(is_file($realPath)) { extract($this->data); require($realPath); } else { throw new \Exception("Template:<code>{$realPath}
Not Found"); } } }
- The function of
require
is not a function- If the
require
is to use the following string as the file name. Regardless of whether the file extension is.php
, the file is considered to be a php program and is introduced into the current program to run.php program is not wrapped by
<?php
and?>
, it will be output directly.
require
will execute the referenced file as a PHP file, no matter what suffix it is (no suffix is acceptable), some PHP Trojans use this to bypass the firewall.
PHP code needs to be placed between<?php
and?>
to be executed.
Crab Demon.
WhenWhen a file is included, the parser leaves PHP mode and enters HTML mode at the beginning of the target file, and resumes at the end of the file. For this reason, any code in an object file that needs to be executed as PHP code must be included within valid PHP start and end tags.
include
orrequrie
a file, if there are no php start tags and end tags in the file<?php ?>
will be parsed as html.