learn PHP Object-oriented -3 carefully
Preface
Ready to write a serious study of the PHP object-oriented series, using PHP to do the Web page, do not have a deep understanding of PHP, may be Sanbang enough, do not need to have much advanced. If there is any mistake, you are welcome to enlighten me. The progress of the arrangement, I learned where to update to where. The form of words on the use of a small case of demand, and then realize, and attach their own summary, article source of the use of the environment
System: ubuntu16.04
Editor: phpstorm2017
PHP7 Requirements: 1 Create an entry file using a custom template Resolution: Ob_start and Get_object_vars and extract use
Ob_start
This function opens the output buffer. When the output buffer is activated, the script will not output the content (except the HTTP headers), but the output needs to be stored in the internal buffer
We can use this function to load a custom template file, but not output to the screen, but to get the data with the variable, and write the data into the created portal file
Get_object_vars
Returns an associative array consisting of object properties
You can get the property value of the object and create the parameter file directory that the user entered and write data to the user
Extract
To import a variable from an array into the current symbol table, you can convert the array directly to the variable (key) = value (key) = value (value) implementation: Create a custom template file (create Index.tpl under Template file) INDEX.TPL
<?php echo ' <?php '?>
/**
* Project name <?php echo $prj _name?>
* User <?php echo $author? &G t;
* Date <?php echo date (' y-m-d ')?> * *
the value of the variable required to write to the template file
god_class.hp
static function Start () {
$config =loadconfig ();
$ff =new god_frame ($config->prj_name);
$ff->prj_name= $config->prj_name;
$ff->author= $config->author;
$ff->run ();
}
in creating a file directory class for input parameters processing and generating templates corresponding to custom files
god_frame.hp
function run () {
!file_exists ($this->project_folder) && mkdir ($this->project_folder);
! File_exists ($this->project_main) && file_put_contents ($this->project_main, ");
Extract (Get_object_vars ($this));
Ob_start ();
Include (DirName (__file__). ' /template/index.tpl ');
$cnt =ob_get_contents ();
Ob_end_clean ();
File_put_contents ($this->project_main, $cnt);
Effect:
Requirements: 2 use PHP built-in Server Deployment Web site for development test use Resolution: System and PHP command line to start the built-in server command
System
Like the C version of the system () function, this function executes the command specified by the command parameter and outputs the execution result.
Start the PHP built-in server
PHP to start the built-in server method
Php-s Directory address for "Address to access"-t project
such as: Php-s localhost:8080-t/home/shisiying/code/phporinentedobject/app implementation: using the system function for execution god_ FRAME.HP
function run () {
!file_exists ($this->project_folder) && mkdir ($this->project_folder);
! File_exists ($this->project_main) && file_put_contents ($this->project_main, ");
Extract (Get_object_vars ($this));
Ob_start ();
Include (DirName (__file__). ' /template/index.tpl ');
$cnt =ob_get_contents ();
Ob_end_clean ();
File_put_contents ($this->project_main, $cnt);
echo "The server is running!";
System ("Php-s localhost:8080-t/home/shisiying/code/phporinentedobject/app");
}
Effect:
It works in the subsequent addition of the build, easy to observe the effect
requirements: 3 for write-only variable file simulation compiler resolution: Scandir and Get_defined_vars () and Var_export use
Scandir
Lists the files and directories in the specified path
Get_defined_vars
Returns an array of all defined variables
Var_export
Outputs or returns a string representation of a variable, and the second argument of the function is set to TRUE, thus returning the representation of the variable. implementation: Create a file
** god_frame.php
function compile () {
$_files=scandir ($this->project_folder. ') /code ');
foreach ($_files as $_file) {
if (Preg_match ("/\w+\.var\.php$/i", $_file)) {
require ($this->project_folder .' /code/'. $_file);
Unset ($_file);
}
Unset ($_files);
$result = ' <?php '. Php_eol
. ' Extract ('. Var_export (Get_defined_vars (), 1). ');
File_put_contents ($this->project_folder. /vars ', $result);
}
god_class.php
Simulate compilation
static function compile () {
$config =loadconfig ();
$GF =new god_frame ($config->prj_name);
$GF->compile ();
}
Effect:
requirements: 4 for write-only function file simulation compiler resolution: Scandir and Get_defined_function () and reflectionfunction use
Scandir
Lists the files and directories in the specified path
Get_defined_vars
Returns an array of all defined variables
Var_export
Outputs or returns a string representation of a variable, and the second argument of the function is set to TRUE, thus returning the representation of the variable.
Get_defined_function ()
Returns an array of all defined functions
Reflectionfunction
The information about a function is reported. Use the following methods
Array_slice
Take a paragraph out of an array, implode the array's intercept function.
Converts the value of a one-dimensional array into a string unset
To release a given variable implementation: Create a function file
god_frame.php
function compile () {
$_files=scandir ($this->project_folder. ') /code ');
foreach ($_files as $_file) {
if (Preg_match ()/\w+\. ( Var|func) \.php$/i ", $_file)" {
require ($this->project_folder. ') /code/'. $_file);
Unset ($_file);
}
Unset ($_files);
$result = ' <?php '. Php_eol
. ' Extract ('. Var_export (Get_defined_vars (), 1). ');
File_put_contents ($this->project_folder. /vars ', $result);
$funs =get_defined_functions ();
foreach ($funs [' user '] as $fun) {
$f =new \reflectionfunction ($fun);
$start = $f->getstartline ();
$end = $f->getendline ();
$fileList =file ($f->getfilename ());
$re = ' <?php '. Php_eol
. Implode (Array_slice ($fileList, $start-1, $end-$start + 1));
File_put_contents ($this->project_folder. /func ', $re);
}
Effect:
requirements: 5 Simulate a simple MVC pattern, receive the video from the browser and perform a simple routing process : $_server[' path_info ' and explode implementations: Create a class file
index.php
$pi =$_server[' Path_info '];
$controller =explode ('/', $PI) [1];
$method =explode ('/', $PI) [2];
Require (GETCWD (). ' /code/'. $controller. " Class.php ");
$get _class=new $controller ();
$get _class-> $method ();
Effect: