Dr. Shen Yi's notes on PHP devil training (8) and Shen Yi's devil
Create a template file:
In this lesson, the teacher led us to create a TEMPLATE. According to the teacher's instruction, we hope to write such a code: 1. For example, I define a variable $ name = ''; 2. Then I read a template. 3. Then I set some "display Formats" in this template ). 4. After loading the template, you can directly replace it with the preceding variables.
1. Create an index. tpl in the template folder. Write the content like this
<? Php echo '<? Php '?> /*** Project name: <? Php echo $ prj_name?> // Do you think it can run? * User: <? Php echo $ prj_author?> // Can it be run? * Date: <? Php echo date ('Y-m-d')?> // Can it? */Echo "hello shenyi";?>
Well, the preparation is done. We will reference it in the class god_frame:
<? Phpnamespace core \ frame; class god_frame {public $ project_folder = ''; // project folder public $ project_main =''; // entry file function _ construct ($ prjName) {// constructor $ this-> project_folder = getcwd (). "/". $ prjName; $ this-> project_main = $ this-> project_folder. "/index. php ";} function run () {// judge and generate a new folder. If no folder exists, create it! File_exists ($ this-> project_folder) & mkdir ($ this-> project_folder ); // obtain external member variables and return the array obtained by this function to the variable list extract (get_object_vars ($ this); Enable PHP's internal buffer (memory) ob_start (); // introduce the template path include (dirname (_ FILE __). '/template/index. tpl '); // obtain the buffer content and assign it to $ cnt = ob_get_contents (); // clear the buffer content ob_end_clean (); // generate an index under this folder. PHP file, create and overwrite 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 content of index. php.
Knowledge Point of anti-spoofing:
_ FILE __:
Dirname ():
Ob_start (): Enable the internal buffer (memory) of PHP ). Put the content to be displayed first in the buffer zone, and do not rush to display it.
Ob_get_contents (); function to obtain the buffer content
Ob_end_clean (); you can clear the buffer content so that no content is output.
Ob_end_flush (); disable the buffer and output the content.
Get_object_vars (); you can get the attribute variable value in the class (instantiated class) and return an array.
Extract ();
Copyright statement: the note organizer loves freedom and advocates sharing. However, this note comes from "PHP devil training first stage" by teacher Shen Yi at www.jtthink.com. This learning note was first launched in the blog Park. If you need to repost it, please respect the work of the teacher and keep the signature of the instructor Shen Yi and the course source address.
Last lesson:Instructor Shen Yi's notes on PHP devil training (7) -- my name