php类自动加载器实现方法

yipeiwu_com6年前PHP代码库

本文实例讲述了php类自动加载器实现方法。分享给大家供大家参考。具体如下:

这里autoload 可兼容以下格式:

Cache_File_Json
class_xxx.php
xxx.class.php
  xxx.php

php代码如下:

function __autoload($className){
 $dirs=explode('_',$className);
 $fileName=array_pop($dirs);
 //print_r($dirs);
 $filePath=$fileName;
 if(is_array($dirs) && (count($dirs) > 0)){
  //echo '\n---\n'; print_r($dirs);
  $dirPath='';
  foreach ($dirs as $dir){
   if($dir){
    $dirPath.=strtolower($dir).DIRECTORY_SEPARATOR;
   }
  }
  $filePath=$dirPath.$fileName.'.php';
 }else {
  if( file_exists('class_'.$fileName.'.php')){
   $filePath='class_'.$fileName.'.php';
  }else {
   if( file_exists($fileName.'.class.php')){
    $filePath=$fileName.'.class.php';
   } else {
    $filePath=$fileName.'.php';
   }
  } 
 }
 //var_dump($filePath);
 require $filePath;
}

希望本文所述对大家的php程序设计有所帮助。

相关文章

php对文件进行hash运算的方法

本文实例讲述了php对文件进行hash运算的方法。分享给大家供大家参考。具体如下: 这段代码非常有用,如果你下载了一个文件,网站提供了hash结果,你可以对你下载下来的文件进行hash运...

php提交表单时判断 if($_POST[submit])与 if(isset($_POST[submit])) 的区别

应该这样用if(isset($_POST['submit'])) { } 提交表单时 if($_POST[submit])与 if(isset($_POST[submit])) 的区别...

php set_time_limit(0) 设置程序执行时间的函数

set_time_limit(0); 括号里边的数字是执行时间,如果为零说明永久执行直到程序结束,如果为大于零的数字,则不管程序是否执行完成,到了设定的秒数,程序结束。 一个简单的例子,...

PHP 缓存实现代码及详细注释

复制代码 代码如下:class CacheException extends Exception {} /** * 缓存抽象类 */ abstract class Cache_Abstr...

php 正则表达式小结

列目录时, dir *.txt或ls *.txt中的*.txt就不是一个正则表达式,因为这里*与正则式的*的含义是不同的。    正则表达式是由普通字符(例如字符 a 到 z)以及特殊字...