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

相关文章

php2html php生成静态页函数

<?php /** ------------------------ Function: php2html($in_Url, $out_htmlFile, $out_logFile...

在PHP程序中使用Rust扩展的方法

 C或PHP中的Rust 我的基本出发点就是写一些可以编译的Rust代码到一个库里面,并写为它一些C的头文件,在C中为被调用的PHP做一个拓展。虽然并不是很简单,但是很有趣。...

php使用filter_var函数判断邮箱,url,ip格式示例

本文实例讲述了php使用filter_var函数判断邮箱,url,ip格式。分享给大家供大家参考,具体如下: 以前使用php的时候还不知道有过滤器filter这玩意,那时候判断邮箱、ur...

php计划任务之验证是否有多个进程调用同一个job的方法

本文实例讲述了php计划任务之验证是否有多个进程调用同一个job的方法。分享给大家供大家参考,具体如下: 在使用计划任务的时候,公司有一次出现过2个进程跑同一个计划任务的情况,导致很多j...

php获取textarea的值并处理回车换行的方法

本文实例讲述了php获取textarea的值并处理回车换行的方法。分享给大家供大家参考。具体实现方法如下: 一般来说,在html表单中textarea中我们按回车与换行都是一些ascii...