Created
July 29, 2018 18:49
-
-
Save ptflp/176fb63b303ce86944e4a91b6b8f6d9c to your computer and use it in GitHub Desktop.
php class autoloader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Autoloader | |
{ | |
class Autoloader | |
{ | |
const debug = 1; | |
public function __construct(){} | |
public static function autoload($file) | |
{ | |
$file = str_replace('\\', '/', $file); | |
$path = $_SERVER['DOCUMENT_ROOT'] . '/classes'; | |
$filepath = $_SERVER['DOCUMENT_ROOT'] . '/classes/' . $file . '.php'; | |
if (file_exists($filepath)) | |
{ | |
if(Autoloader::debug) Autoloader::StPutFile(('подключили ' .$filepath)); | |
require_once($filepath); | |
} | |
else | |
{ | |
$flag = true; | |
if(Autoloader::debug) Autoloader::StPutFile(('начинаем рекурсивный поиск')); | |
Autoloader::recursive_autoload($file, $path, $flag); | |
} | |
} | |
public static function recursive_autoload($file, $path, $flag) | |
{ | |
if (FALSE !== ($handle = opendir($path)) && $flag) | |
{ | |
while (FAlSE !== ($dir = readdir($handle)) && $flag) | |
{ | |
if (strpos($dir, '.') === FALSE) | |
{ | |
$path2 = $path .'/' . $dir; | |
$filepath = $path2 . '/' . $file . '.php'; | |
if(Autoloader::debug) Autoloader::StPutFile(('ищем файл <b>' .$file .'</b> in ' .$filepath)); | |
if (file_exists($filepath)) | |
{ | |
if(Autoloader::debug) Autoloader::StPutFile(('подключили ' .$filepath )); | |
$flag = FALSE; | |
require_once($filepath); | |
break; | |
} | |
Autoloader::recursive_autoload($file, $path2, $flag); | |
} | |
} | |
closedir($handle); | |
} | |
} | |
private static function StPutFile($data) | |
{ | |
// $dir = $_SERVER['DOCUMENT_ROOT'] .'/Log/Log.html'; | |
// $file = fopen($dir, 'a'); | |
// flock($file, LOCK_EX); | |
// fwrite($file, ('║' .$data .'=>' .date('d.m.Y H:i:s') .'<br/>║<br/>' .PHP_EOL)); | |
// flock($file, LOCK_UN); | |
// fclose ($file); | |
} | |
} | |
spl_autoload_register('Autoloader\Autoloader::autoload'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment