How to automatically load class files

Source: Internet
Author: User
Tags autoload
How to automatically load class files! Currently, autoload (the code is as follows:) is there any other way to automatically load class files? Except include (specific file path) PHPcodefunction _ autoload ($ className) {if (file_exists (model. $ className .. class. php) {include_once (how does the model automatically load class files!
Currently, autoload (the code is as follows:) is there any other way to automatically load class files? Except include (specific file path)
PHP code
  function __autoload($className){if(file_exists('model/'.$className.'.class.php')){include_once('model/'.$className.'.class.php');}if(file_exists('Controller/'.$className.'.class.php')){include_once('Controller/'.$className.'.class.php');}$prefix=$GLOBALS['groupName']=='main'?'':'../';if(file_exists($prefix.'core/'.$className.'.class.php')){include_once($prefix.'core/'.$className.'.class.php');}if(file_exists($prefix.'core/Smarty-3.1.6/'.$className.'.class.php')){include_once($prefix.'core/Smarty-3.1.6/'.$className.'.class.php');}if(file_exists($prefix.'core/Smarty-3.1.6/sysplugins/'.$className.'.php')){include_once($prefix.'core/Smarty-3.1.6/sysplugins/'.$className.'.php');}}


------ Solution --------------------
Do you think this is troublesome?
In fact, it is because you put many class files, and you should use if else if or switch... case. Statement.
If you are too troublesome, you can use an array to save all the paths, for example.
PHP code
$arr = array('classPath1','classPath2','classPath3');foreach($arr as $v){  $new_path = $v.$className.'.php';  if(file_exists($new_path)){    include_once($new_path);    break;  }} 

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.