Carefully learn PHP object-oriented-4

Source: Internet
Author: User
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:

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.