php类自动加载器实现方法

yipeiwu_com5年前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 error_log 函数的使用

我们来大致了解一下error_log()函数。我们看下手册的解释: error_log(PHP 3, PHP 4, PHP 5) bool error_log ( string mess...

c#中的实现php中的preg_replace

把php preg_replace 用c# 重写了一下。 PHP语言的功能非常强大,主要就是靠它强大的函数来作支撑。我们在这篇文章中将会为大家详细讲解有关PHP函数preg_replac...

PHP写的加密函数,支持私人密钥(详细介绍)

在开发PHP系统时,会员部分往往是一个必不可少的模块,而密码的处理又是不得不面对的问题,PHP 的 Mcrypt 加密库又需要额外设置,很多人都是直接使用md5()函数加密,这个方法的确...

11个PHPer必须要了解的编程规范

本文将讨论常用的良好的代码习惯,或者称为代码规范,在PHP领域。 1,错误报告开启 错误报告是在PHP中一个非常有用的功能,应同时在开发阶段启用。 这可以帮助我们确定我们的代码中的问题。...

浅谈PHP中Stream(流)

流(stream)的概念源于UNIX中管道(pipe)的概念。在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备、外部文件等。根据流的方向又可以分为输入流...