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 Google的translate API代码

新建一个ANSI的PHP文件,然后创建一个类: 复制代码 代码如下:header("Content-Type: text/html; charset=utf-8"); class Goo...

探讨PHP JSON中文乱码的解决方法详解

我们知道在使用Ajax技术与PHP后台交互时,中文乱码是常有的事,JSON作为与XML类似的数据交换格式,在PHP用来进行交互时也会出现中 文乱码的情况,解决PHP JSON中文乱码的方...

PHP 字符串加密函数(在指定时间内加密还原字符串,超时无法还原)

这样我们就可以拿此函数来做很多用途了,比如:单点登录的token加密传输啦,临时密码啦等等复制代码 代码如下: /** * @param string $string 原文或者密文 *...

PHP反射机制原理与用法详解

本文实例讲述了PHP反射机制原理与用法。分享给大家供大家参考,具体如下: 反射 面向对象编程中对象被赋予了自省的能力,而这个自省的过程就是反射。 反射,直观理解就是根据到达地找到出发地和...

php修改时间格式的代码

修改时间格式: date("Y-m-d",strtotime($list['pubdate'])); 学习解释:将时间放入strtotime为时间戳后用date()转化格式.下面写了两个...