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中CURL的CURLOPT_POSTFIELDS参数使用细节

在通常情况下,我们使用 CURL 来提交 POST 数据的时候,我们已经习惯了这样的写法:复制代码 代码如下:curl_setopt( $ch, CURLOPT_POSTFIELDS,$...

PHP从FLV文件获取视频预览图的方法

本文实例讲述了PHP从FLV文件获取视频预览图的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下:<?php // references http://w...

PHP版QQ互联OAuth示例代码分享

由于国内QQ用户的普遍性,所以现在各大网站都尽可能的提供QQ登陆口,下面我们来看看php版,给大家参考下 /** * QQ互联 oauth * @author dyllen *...

PHP使用in_array函数检查数组中是否存在某个值

本文实例讲述了PHP使用in_array函数检查数组中是否存在某个值的方法。分享给大家供大家参考。具体分析如下: PHP使用in_array()函数检查数组中是否存在某个值,如果存在则返...

当前比较流行的两款PHP加密、解密工具Zend Guard和iconCube介绍

当前比较流行的两款PHP加密、解密工具Zend Guard和iconCube介绍

当前市场上较流行的对PHP进行上述加密授权的软件主要有二种: (1)Zend公司的ZendGuard。 (2)ionCube公司的ionCube PHP Encode。 ZendGuar...