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获取文章内容第一张图片的方法示例

本文实例讲述了php获取文章内容第一张图片的方法。分享给大家供大家参考,具体如下: <?php $temp=mt_rand(1,4); $pattern="/<[...

php下安装配置fckeditor编辑器的方法

一、PHP调用fckeditor方法。 二、JS调用fckeditor方法。 复制代码 代码如下: <?php require_once(PATH_PRE.”fckeditor.p...

php 设计模式之 工厂模式

本人常用mysql数据库,所以程序只写了mysql的数据库操作类。希望各位高手把另外的类写全,最好能发一份给我。 db_mysql.php继承db.php接口,具体实现数据库操作的各种方...

php实现根据词频生成tag云的方法

本文实例讲述了php实现根据词频生成tag云的方法。分享给大家供大家参考。具体如下: 这里给定一段文本,分析文本的词频分布,生成tag云 <?php /** * Ta...

ADODB结合SMARTY使用~超级强

Smarty实例教学实例篇(三、使用ADODB连接数据库) 前两个月因为工作上的原因一直很忙,所以没有及时完成这个教程,正好今天周六不用加班,抽个空完成它吧! 在开始新的的教程...