php自动加载类使用:
目录结构
index的代码展示
<?php
header("Content-type:text/html;charset=utf-8"); //设置框架编码
ini_set("date.timezone", "Asia/Shanghai");//设置 时间区域
const APP_PATH = __DIR__ . '/';//定义我们的项 目路径常量
const Lib = '../PHPmm/';//定义我们的框架目录常量 //注意路径
const Resource = APP_PATH . 'Resource';//定义 我们的项目资源目录常量
ini_set('display_errors', 1);//是否抛出错误
/**
* display_errors介绍:错误回显,一般常用语开发 模式,但是很多应用在正式环境中也忘记了关闭 此选项。错误回显可以暴露出非常多的敏感信息, 为攻击者下一步攻击提供便利。推荐关闭此选项。 display_errors = On 开启状原南,若出现错误, 则报错,出现错误提示 dispaly_errors = Off 关闭 状态下,若出现错误,则提示:服务器错误。但 是不会出现错误提示**/
require Lib . 'PHPmm.php'; //注意路径
//运行框架行为
$app = new PHPmm();
$app->run();
$app = null;
PHPmm.php代码展示
<?php
class PHPmm
{
//框架运行方法
public function run()
{
spl_autoload_register(array($this, 'load'));//注册给定的函数作为 __autoload 的实现
$testa = new a();
// $jobb = new b();
$testa->b();
// $jobb->hello();
}
private function load($className)
{
require Lib . $className . '.class.php';
}
}
a.class.php的代码展示
<?php
class a
{
public function b()
{
echo 'hello ,你好';
}
}
b.class.php的代码展示
<?php
class b
{
public function hello()
{
echo 'PHPmm';
}
}
如果
// $jobb = new b();
// $jobb->hello();
被注释的话
运行效果
为 hello ,你好
否则 hello ,你好PHPmm
成功学习phpphp自动加载类使用