1.使用spl_autoload_register函数注册一个自己定义的自动加载函数
2.当在代码中new一个不存在的类的时候,上面的函数会被调用,不存在的类名会被作为参数传入该函数中
3.兼容了两种方式,命名空间对应目录的方式 \app\test,类名下划线分割对应目录的方式app_test,都是对应的app目录下的test.php文件,类名要和文件名一致
4.set_include_path(),可以有多个用冒号:隔开,动态设置php.ini中的include_path 配置选项

<?php
class Application {
private static $instance = null;
private $libPath = './';
private $phpExt = '.php';
public function setLibPath($path, $autoload = false) {
$this->libPath = trim(trim($path), DIRECTORY_SEPARATOR);
set_include_path($this->getLibPath());
if ($autoload) {
spl_autoload_register(array('Application', 'load'));
}
return $this;
}
public static function instance() {
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
//获取文件后缀
public function getPhpExt() {
return $this->phpExt;
}
//设置文件后缀
public function setPhpExt($ext) {
$this->phpExt = $ext;
return $this;
}
//设置根路径
public function setPath($path) {
$this->path = rtrim(trim($path), DIRECTORY_SEPARATOR);
return $this;
}
//设置自动加载的路径
public function getLibPath() {
return $this->path . DIRECTORY_SEPARATOR . $this->libPath;
}
//自动加载函数
public static function load($class) {
$pos = strrpos($class, '\\');
if ($pos !== false) {
$ns = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos + 1));
$className = substr($class, $pos + 1);
} else {
$ns = DIRECTORY_SEPARATOR;
$className = $class;
}
if (strpos($className, '_') !== false) {
$className = str_replace('_', DIRECTORY_SEPARATOR, $className);
}
$ins = self::instance();
$classFile = $ins->getLibPath() . $ns . $className . $ins->getPhpExt();
if (!(include $classFile)) {
throw new Exception('load class failed: class=' . $class . ' file=' . $classFile);
}
}
}
Application::instance()->setPath(dirname(__FILE__))->setLibPath(DIRECTORY_SEPARATOR, true);
//测试,在根目录创建app目录,下面创建这两个文件
new app_user();
new \app\admin();

app目录下面的user.php

<?php
class app_user{
public function __construct(){
new \app\admin();
}
}

app目录下的admin.php

<?php
namespace app;
class admin{}

[PHP] 项目实践中的自动加载实现的更多相关文章

  1. PHP中的自动加载

      自动加载? 或许你已经对自动加载有所了解.简单描述一下:自动加载就是我们在new一个class的时候,不需要手动去写require来导入这个class.php文件,程序自动帮我们加载导入进来.这是 ...

  2. 关于thinkPHP中的自动加载和手动导入

    首先先讲自动加载: 前提:你的第三方类库要满足(1)符合命名规范和后缀的类库(2)使用命名空间,命名空间和路径一致的类库 (1)在ThinkPHP目录下的library目录下的每一个子目录都是一个根命 ...

  3. autocad2008+C#2008开发中设置自动加载dll

    一.复制编译后的dlll路径,比如我的是D:\zjy\cad开发\学习\宗地图\bin\Debug\zd.dll 二.随便找个地方新建一个记事本,在记事本中写入以下内容: (command " ...

  4. ZendFramework中实现自动加载models

    最近自学Zendframework中,写Controller的时候总要require model下的类文件,然后才能实例化,感觉非常不爽 Google了许久,找到个明白人写的方法不错,主要就是修改ap ...

  5. 在Thinkphp中【自动加载自定义扩展配置文件】!

    /Conf/config.php为正式的扩展文件 /Conf/verify.php为扩展的自定义配置文件 /Conf/sendmail.php为扩展的自定义配置文件 如果要自动加载 verify和se ...

  6. 访问项目时,不能自动加载index.php文件

    1.修改配置文件D:\lamp\apache\conf\httpd.conf加上DirectoryIndex index.hmtl index.php <IfModule !mpm_netwar ...

  7. PHP 命名空间以及自动加载(自动调用的函数,来include文件)

    这篇文章的目的是记录 1. php中的自动加载函数 __autoload(), 和 spl_autoload_register()函数, 2 .php中命名空间的使用. 一.当不使用命名空间的时候 a ...

  8. Composer 自动加载(autoload)机制

    自动加载的类型 总体来说 composer 提供了几种自动加载类型 classmap psr-0 psr-4 files 这几种自动加载都会用到,理论上来说,项目代码用 psr-4 自动加载, hel ...

  9. php怎么自动加载

    在 PHP 代码的顶部你是不是经常看到这样的代码. require 'lionis.php'; require 'is.php'; require 'cool.php'; 如果只是引入几个 PHP 脚 ...

随机推荐

  1. Flask request接口获取参数

    Flask request接口获取参数   request.form.get("key", type=str, default=None) 获取表单数据, request.args ...

  2. 12Java基础_数组定义格式/动态初始化/静态初始化

    /* Java数组 格式一: int[] array; 格式二: int array[]; 数组初始化: 为数组中的元素分配内存空间 动态初始化: int[] array=new int[数组长度] ...

  3. luoguP1972 [SDOI2009]HH的项链

    经典颜色问题推荐博文 https://www.cnblogs.com/tyner/p/11519506.html https://www.cnblogs.com/tyner/p/11616770.ht ...

  4. mysql 日期处理函数

    首先创建一张实验用的一张表 drop table if exists t_student; create table t_student( id int primary key auto_increm ...

  5. Codeforces Rating System

    来翻译一下官方文档,但是建议看英文原文,本文可能会出现一些错误,虽然不是为了方便自己查阅用的. 首先,对于人 \(i\),定义 \(r_i\) 是他的 rating,对于人 \(i,j\),定义 \( ...

  6. c# 多线程 双色球

    学习记录.仅供参考. 知识点: 多线程 Lock 环境: Visual Studio 2017 public partial class Form1 : Form { private static r ...

  7. Unreal Engine 4 系列教程 Part 8:粒子系统教程

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  8. Redis集群模式介绍

    前言: 一.为什么要使用redis 1,解决应用服务器的cpu和内存压力 2,减少io的读操作,减轻io的压力(内存中读取) 3,关系型数据库扩展性,不强,难以改变表的结构 二.优点 1,nosql数 ...

  9. 在Azure DevOps Server (TFS)的流水线中编译和测试Xcode移动应用(iPhone)

    概述 Xcode是开发基于苹果macOS系统的桌面应用和移动应用的主要IDE工具.使用Azure DevOps Server (原名TFS)系统中的pipelines流水线功能,可以方便的集成Xcod ...

  10. LeetCode 21:合并两个有序链表 Merge Two Sorted Lists

    将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. Merge two sorted linked lists and return it as a new ...