How to load class files automatically

Source: Internet
Author: User
How to load class files automatically!
Now with AutoLoad (code as follows:), is there any other way to load the class file automatically? In addition to 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 more troublesome?
This is because your class files are put in more places, and you should use if else if or switch...case. Statement.
If you're in trouble, you can use arrays to save all the paths, like.
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  
  • Related Article

    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.