Instructor Shen Yi's notes on PHP devil training (7) -- my name is Shen Yi's devil

Source: Internet
Author: User
Tags autoload

Instructor Shen Yi's notes on PHP devil training (7) -- my name is Shen Yi's devil

1. Generate a folder.

Mkdir (); -- create a directory
Bool mkdir (string $ pathname [, int $ mode = 0777 [, bool $ recursive = false [, resource $ context])
// Try to create a directory specified by pathname.

Parameters:pathname:Directory path.

  mode:The default mode is 0777, which means the maximum possible access. For more information about mode, see the chmod () page.

Have you seen the above function? Remember. In the previous lesson, Mr. Shen left a job to read the god. json file and generate the simplest "skeleton ". 1. The start parameter is accepted. 2. Generate a folder Based on the prj_name value. 3. An index. php file is generated by default in the new folder.

OK. Let's first implement the first step. Write a method in the godinit file.

 static function start(){        $get_config = loadConfig();        mkdir(getcwd()."/".$get_config->prj_name);    }

 

 

Then./god start. In my file path, a project folder is generated. Of course, this is a demo of the course. We also need to determine whether the folder already exists and create it if it does not exist. If it exists, it will not be created.

Improve the start () method:

static function start(){        $get_config = loadConfig();        !file_exists(getcwd()."/".$get_config->prj_name) && mkdir(getcwd()."/".$get_config->prj_name);    }

 

The preceding function: file_exists (); -- checks whether a file or directory exists. Let's further strengthen:

Bool file_exists (string $ filename) // check whether a file or directory exists.

 

Parameters:filename File or directory path.

Return Value: IffilenameIf the specified file or directory existsTRUEOtherwise, returnFALSE.

 

We continue to go back to the course and generate a PHP file:

Static function start () {$ get_config = loadConfig (); // judge and generate a new folder. If no folder exists, create it! File_exists (getcwd (). "/". $ get_config-> prj_name) & mkdir (getcwd (). "/". $ get_config-> prj_name); // you can check the folder and generate an index. PHP file. Create it if no file exists! File_exists (getcwd (). "/". $ get_config-> prj_name. "/index. php ") & file_put_contents (getcwd (). "/". $ get_config-> prj_name. "/index. php ","");}

 

OK. The homework of the previous lesson is completed.

 

The main content of this lesson is to create a god_frame.php class and write a class dedicated to processing the skeleton.

Since god is used for "skeleton. Then we need to create a constructor to predefine the folder Name of the skeleton and the entry file of the skeleton. Then we need to create a core folder that represents the god kernel, create a sub-folder named frame below, and then create a folder named template under the frame. Finally, put god_frame.php under the frame.

Before writing code, we need to strengthen a magic function:

_ Autoload (); -- try to load undefined classes

Void _ autoload (string $ class) // you can define this function to enable automatic loading of classes.

Parameters:class--Name of the class to be loaded

// Example // try to load an undefined class. If an undefined class is loaded, the function will automatically enter the function (if you have written it) function _ autoload ($ classname) // receives a parameter {echo $ classname; through which you can find that the "Class Name" of the class you are trying to load will be obtained}

Then we will study namespace today. Use to import the namespace.

Okay. Let's take a look at the Code completed in this lesson: god_frame.php.

<? 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); // you can locate and generate an index. php file in this folder. If no file exists, create it! File_exists ($ this-> project_main) & file_put_contents ($ this-> project_main, "") ;}}?>
// Godinit <? Phpdefine ('cstring', 'json'); require ('godconfig. php '); // introduce the gonconfig file use core \ frame; function _ autoload ($ className) {$ className = str_replace ('\\','/', $ className ). '. php '; require ($ className);} class godinit // create a class, godinit {static $ v = "god version is 1.2 "; // declare a static attribute $ VERSION static function init () // static method init {echo "input your project name? ". PHP_EOL; $ prj_name = fgets (STDIN); // obtain user input again and assign it to $ prj_name echo" input your author name? ". PHP_EOL; $ prj_author = fgets (STDIN); // obtain user input again and assign it to $ prj_author genConfig (TC (array ('prj _ name' => $ prj_name, 'prj _ author' => $ prj_author);} function ini () {$ get_config = loadConfig (); foreach ($ get_config as $ k => $ v) echo $ k. ":". $ v;} static function start () {$ get_config = loadConfig (); $ gf = new god_frame ($ get_config-> prj_name); $ gf-> run ();} /* static function make () {$ pchar = new Phar ("god. phar "); $ Pchar-> buildFromDirectory (dirname (_ FILE _); $ pchar-> setStub ($ pchar-> createDefaultStub ('God ')); $ pchar-> compressFiles (Phar: GZ);} */static function _ callStatic ($ p1, $ p2) {echo "error function" ;}}?>

 

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 (6) -- Witchcraft and skeleton

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.