In this class, the teacher led us to create TEMPLATE. According to the teacher's teaching, we hope to write the code in this way: 1. For example, I define a variable $name=‘’; 2. Then I read a template. 3. Then I set some of my own "display formats" (hard style) in this template. 4. After loading the template, you can directly replace it with the above variables.
1. First create a new index.tpl in the template folder. The content is written like this
<?php echo '<?php' ?> /** * project name: <?php echo $prj_name ?> //大家想想, 它能運行嗎? *User: <?php echo $prj_author ?> //能運行嗎? *Date: <?php echo date('Y-m-d')?> //能嗎? */ echo "hello shenyi"; ?>
Okay, the preparations are done, we reference it in the god_frame class:
<?php namespace core\frame; class god_frame { public $project_folder = ''; //項目文件夾 public $project_main = ''; //入口文件 function __construct($prjName){ //構(gòu)造函數(shù) $this->project_folder = getcwd()."/".$prjName; $this->project_main = $this -> project_folder."/index.php"; } function run(){ //判斷并生成新的文件夾,沒有就創(chuàng)建 !file_exists($this->project_folder) && mkdir($this->project_folder); //獲取外部成員變量并把該函數(shù)獲取的數(shù)組返回成變量列表 extract(get_object_vars($this)); 開啟PHP的內(nèi)部緩沖區(qū)(內(nèi)存) ob_start(); //引入模板路徑 include (dirname(__FILE__).'/template/index.tpl'); //獲取緩沖區(qū)的內(nèi)容,并賦給$cnt $cnt =ob_get_contents(); //清理緩沖區(qū)內(nèi)容 ob_end_clean(); //在該文件夾下生成一個index.php文件,沒有就創(chuàng)建并覆蓋 file_put_contents($this->project_main,"$cnt"); } } ?>
I also need to improve the start (method) in godinit
static function start(){ $get_config = loadConfig(); $gf = new god_frame($get_config->prj_name); $gf -> prj_name = $get_config->prj_name; $gf -> prj_author = $get_config->prj_author; $gf -> run(); }
Execute this method in the command line
Then let’s take a look at the directory structure of the entire document and the contents of index.php
Added knowledge points:
__FILE__:
dirname():
ob_start(): Start PHP internal buffer (memory). Put the content to be displayed next into the buffer first, and display it without haste.
ob_get_contents(); function to get the contents of the buffer
ob_end_clean(); You can clear the buffer contents, so that Output content
ob_end_flush(); Close the buffer and output the content
get_object_vars(); You can get the attribute variable values ????in the class (the instantiated class) and return the array.
extract();

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)