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+javascript实现二级级联菜单的制作

 大体思路是这样的:为了不让先前的页面刷新,我用iframe潜入了一个二级子页面,用来读取数据库中的数据,最后把想要的数据传递给父级页面,完成数据的选择和转移。 主要程序代码如...

php无限分类使用concat如何实现

一、数据库设计 -- -- Table structure for table `category` -- CREATE TABLE `category` ( `...

计算一段日期内的周末天数的php代码(星期六,星期日总和)

复制代码 代码如下: /* | Author: Yang Yu <niceses@gmail.com> | @param char|int $start_date 一个有效的...

PHP使用ob_start生成html页面的方法

本文实例讲述了PHP使用ob_start生成html页面的方法。分享给大家供大家参考。具体方法分析如下: ob_start([string output_callback])- 打开输出...