php自动加载指定目录下的类文件

From: https://www.cnblogs.com/CpNice/p/4369399.html

网上找到的类,非常有用,文件名: autoload.php

<?php
/**
 * Created by PhpStorm.
 * User: zcm
 * Mail: [email protected]
 * Date: 2018/10/10 上午10:20
 */

if(!defined('ROOTDIR'))
{
	define('ROOTDIR', realpath(__DIR__ . '/../'));
}

class Autoloader {

	public static function myAutoload( $name )
	{
		$class_path = str_replace('\\',DIRECTORY_SEPARATOR, $name);
		$file = ROOTDIR . '/' . $class_path . '.php';
		if( file_exists( $file ) )
		{
			require_once( $file );
			if( class_exists($name, false) )
			{
				return true;
			}
		}
		return false;
	}
}

spl_autoload_register('Autoloader::myAutoload');

使用时,只要require/require_once 此文件即可,想用的类即可找到了!

猜你喜欢

转载自blog.csdn.net/JoeBlackzqq/article/details/82995380