php类自动加载器实现方法

yipeiwu_com5年前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程序设计有所帮助。

相关文章

静态html文件执行php语句的方法(推荐)

HTM文件中的PHP语句不会被执行,如何在HTML文件中运行php代码? html文件执行php语句的方法: 1,修改httpd.conf,命令Apache把HTML当作PHP, 需要修...

php mssql 时间格式问题

当然是在程序里解决比较灵活,例如: “select convert(char,日期字段,120) as date2 from table” convert(char,date,N)输出的...

为何说PHP引用是个坑,要慎用

前言 去年我参加了很多次会议,其中八次会议里我进行了相关发言,这其中我多次谈到了 PHP 的引用问题,因为很多人对它的理解有所偏差。在深入讨论这个问题之前,我们先回顾一下引用的基本概念,...

php微信开发自定义菜单

php微信开发自定义菜单

目前微信服务号自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替。请注意,创建自定义菜单后,由...

PHP基于imagick扩展实现合成图片的两种方法【附imagick扩展下载】

本文实例讲述了PHP基于imagick扩展实现合成图片的两种方法。分享给大家供大家参考,具体如下: 方法一:compositeimages /** * function: 合成图片...