learn PHP Object-oriented -4 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 requirements: 1 simulation of the generation of routing resource files
We often use the framework to contact the concept of routing, today we will simulate the creation of simple routing resource files to explore how the routing resource files generated. Solution: Use of get_declared_classes (), _class_,array_search, and reflective reflectionclass,getdoccomment
The above function does not understand can look for its usage in the official website to create index.php
god_frame.php
Gets all the loaded classes
$class _set=get_declared_classes ();
$class _set=array_slice ($class _set,array_search (__class__, $class _set) +1);
$result =array ();
foreach ($class _set as $class) {
$mvc =new god_mvc ($class);
if ($mvc->iscontroller ()) {
$mp = $mvc->getrequestmappings ();
$result =array_merge ($result, $MP);
}
Generate Routing file
file_put_contents ($this->project_folder. ' /request_route ', ' <?php return '. Var_export ($result, 1).
New Class god_mvc.php
<?php namespace Core\frame;
Class God_mvc {Public $classComment = "";
Public $classMethods =array ();
Public $className = "";
function __construct ($cname) {$this->classname= $cname;
$Rf =new \reflectionclass (($cname)); $this->classcomment= $Rf->getdoccomment ()//Get the annotation of the class $this->classmethods= $Rf->getmethods ();//Get all the inside of the class Method Collection, returning a method object array} function Iscontroller () {return Preg_match ("/@Controller/", $this-&G
T;classcomment);
function Getrequestmappings () {$result =array ();
foreach ($this->classmethods as $method) {$get _res= $this->getrequestmappingsresult ($method);
if ($get _res) {$result =array_merge ($result, $get _res);
} return $result;
} function Getrequestmappingsresult ($m) { if (Preg_match ('/@RequestMapping \ (? <requesturl>.{
3,20}) ", Method= (? <requestmethod>\w{3,8}) \)/', $m->getdoccomment (), $result)) {return Array (
$result [' Requesturl ']=>array (' Requestmethod ' => $result [' Requestmethod '],
' Class ' => $this->classname, ' method ' => $m->getname ()
)
);
return false;
}
}
Effect: