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字符串过滤strip_tags()函数用法实例分析

本文实例讲述了php字符串过滤strip_tags()函数用法。分享给大家供大家参考,具体如下: strip_tags — 从字符串中去除 HTML 和 PHP 标记,非常重要的函数 (...

PHP中通过HTTP_USER_AGENT判断是否为手机移动终端的函数代码

有时候很实用在一些场合,留住备用吧 复制代码 代码如下:function is_mobile_request()       {&nbs...

mongodb和php的用法详解

Mognodb数据库连接. 默认格式 $m = new Mongo(); //这里采用默认连接本机的27017端口,当然也可以连接远程主机如 192.168.0.4:27017,如...

PHP中把对象转换为关联数组代码分享

/** * 对象转关联数组 * @author * @param object $obj * @return array */ function object_to_a...

php代码审计比较有意思的例子

php代码审计比较有意思的例子

代码审计比较有意思的例子貌似是去年 ecshop支付漏洞偶然出来的一个例子,感觉不错。分享下 复制代码 代码如下:<?php$a=addslashes($_GET['a']);$b...