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 goto语句用法实例

问题 当 PHP 在执行代码过程,在某一时刻我们希望它能跳转到某一特定位置继续执行代码,该怎么做呢? 回答 在 PHP 中,我们可以使用 goto 操作符来使 PHP 代码执行器跳转到...

使用迭代器 遍历文件信息的详解

1.迭代文件的行复制代码 代码如下:        public static IEnumerable<str...

php XPath对XML文件查找及修改实现代码

复制代码 代码如下: <?php /* <?xml version="1.0" encoding="utf-8"?> <article> <item&...

PHP文件上传操作实例详解

PHP文件上传操作实例详解

本文实例分析了PHP文件上传操作。分享给大家供大家参考,具体如下: 文件上传 发生在浏览器向服务器发出的请求中。 文件,对于浏览器来讲,就是表单中的一个特殊类型的数据而已。 浏览器表单中...

浅析PHP原理之变量分离/引用(Variables Separation)

首先我们回顾一下zval的结构:复制代码 代码如下:struct _zval_struct {       &nbs...