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基于socket实现客户端和服务端通讯功能

本文主要介绍了PHP基于socket实现的简单客户端和服务端通讯功能,可实现服务端接收客户端发送的字符串进行翻转操作后返回客户端的功能,需要的朋友可以参考下 服务端: <...

PHP函数import_request_variables()用法分析

本文实例分析了PHP函数import_request_variables()用法。分享给大家供大家参考,具体如下: import_request_variables 函数可以在 regi...

php中数组首字符过滤功能代码

复制代码 代码如下: <?php $array = array( 'abcd', 'abcde', 'bcde', 'cdef', 'defg', 'defgh' ); $str...

PHP空值检测函数与方法汇总

几乎任何入口的HTTP请求我们都会去检测它携带的参数,类似 isset() empty() 的函数一定不少见。 以下的测试结果基于 PHP7.16 是否定义判断: isset() 可以...

PHP使用栈解决约瑟夫环问题算法示例

本文实例讲述了PHP使用栈解决约瑟夫环问题算法。分享给大家供大家参考,具体如下: 约瑟夫环问题: 39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌...