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提交表单时判断 if($_POST[submit])与 if(isset($_POST[submit])) 的区别

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

PHP中new static() 和 new self() 的区别介绍

长夜漫漫啊! 今天领导本地搭建一个站。发现用PHP 5.2 搭建不起来,站PHP代码里面有很多5.3以上的部分,领导让苦逼我更改在5.2下能运行。 改着改着发现了一个地方 复制代码 代码...

PHP学习记录之面向对象(Object-oriented programming,OOP)基础【类、对象、继承等】

PHP学习记录之面向对象(Object-oriented programming,OOP)基础【类、对象、继承等】

本文实例讲述了PHP学习记录之面向对象(Object-oriented programming,OOP)基础。分享给大家供大家参考,具体如下: 在面向对象的程序设计(英语:Object-...

PHP源代码数组统计count分析

zend给php的所有变量都用结构的方式去保存,而字符串的保存和数组的保存也是不同的,数组采用的是hash表的方式去保存(大家知道hash保存的地址有效的减少冲突-hash散列表的概念你...

老生常谈PHP位运算的用途

在实际应用中可以做用户权限的应用 我这里说到的权限管理办法是一个普遍采用的方法,主要是使用到”位运行符”操作,& 位与运算符、| 位或运行符。参与运算的如果是10进制数,则会被转换至2进...