The "25" tool class implements auto-loading and class

Source: Internet
Author: User

We have created the corresponding directory in the previous section, but we found that the class we created did not run successfully, why? This is mainly because the location of the class file we loaded is not the result, we have previously loaded the controller according to the class name, so we need to make a decision here:

Private Static function_autoload ($className){        Switch(true) {        //Judging is the controller//controller             Case strlen($className) >10 &&substr($className, -10) = = ' Controller ':$path= App_controller_path. ' /‘.$className.‘. Class.php '; if(!Is_file($path)) Halt ($path.‘ Controller not found '); include $path;  Break; default:$path= Tool_path. ' /‘.$className.‘. Class.php '; if(!Is_file($path)) Halt ($path.‘ Class not found '); include $path;  Break; }    }

Here is a brief explanation of the type that is currently loaded based on the name of the class passed in, if the length of the class is greater than 10 and the last 10 bits equals ' Controller ', then it shows that the current load is a class of controllers, we can set the path of the controller, of course, before the load to determine whether it is a file, if not meet the above conditions, then go to the default (that is, load the custom class), there may be other types later, and then add judgment can 。

3. Here, the tool class implements automatic loading and the class is complete, try it:

$Code =new Code ();//Instantiate Class $Code->show ();//Call Show method in class

Is it normal to show it?

The "25" tool class implements auto-loading and class

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.