


PHP automatic loading method collection, PHP loading method collection_PHP tutorial
php自动加载方式集合,php加载方式集合
php加载文件方式:
1、include,include_once,requice,requice_one常规加载
2、__autoload()
3、spl_autoload_register()
常规加载方式
假设我们有一个类文件A.php,里面定义了一个名字为A的类:
<?php class A { public function __construct() { echo 'Got it.'; } }
然后我们有一个index.php需要用到这个类A,常规的写法就是
<?php require('A.php'); $a = new A();
但是有一个问题就是,假如我们的index.php需要包含的不只是类A,而是需要很多类,这样子就必须写很多行require语句,有时候也会让人觉得不爽。
__autoload()自动加载
不过在php5之后的版本,我们就不再需要这样做了。
在php5中,试图使用尚未定义的类时会自动调用__autoload函数,所以我们可以通过编写__autoload函数来让php自动加载类,而不必写一个长长的包含文件列表。
例如在上面那个例子中,index.php可以这样写:
<?php function __autoload($class){ $file = $class . '.php'; if (is_file($file)) { require_once($file); } } $a = new A();
当然上面只是最简单的示范,__autoload只是去include_path寻找类文件并加载,我们可以根据自己的需要定义__autoload加载类的规则。注意:由于__autoload()是个函数,只能存在一次。
spl_autoload_register()自动加载
但现在问题来了,如果在一个系统的实现中,如果需要使用很多其它的类库,这些类库可能是由不同的开发人员编写的,其类名与实际的磁盘文件的映射规则不尽相同。这时如果要实现类库文件的自动加载,就必须在__autoload()函数中将所有的映射规则全部实现,这样的话__autoload()函数有可能 会非常复杂,甚至无法实现。最后可能会导致__autoload()函数十分臃肿,这时即便能够实现,也会给将来的维护和系统效率带来很大的负面影响。在这种情况下,难道就没有更简单清晰的解决办法了吧?答案当然是:NO!
spl_autoload_register() 满足了此类需求。 它实际上创建了 autoload 函数的队列,按定义时的顺序逐个执行。相比之下, __autoload() 只可以定义一次。
bool spl_autoload_register ([ callable $autoload_function [, bool $throw = true [, bool $prepend = false ]]] )
我们继续改写上面那个例子:
<?php function loader($class){ $file = $class . '.php'; if (is_file($file)) { require_once($file); } } spl_autoload_register('loader'); $a = new A();
或者直接使用匿名函数:
<?php spl_autoload_register(function($file){ $file = $class . '.php'; if (is_file($file)) { require_once($file); } }); $a = new A();
这样子也是可以正常运行的,这时候php在寻找类的时候就没有调用__autoload而是调用我们自己定义的函数loader了。同样的道理,下面这种写法也是可以的:
<?php class Loader { public static function loadClass($class){ $file = $class . '.php'; if (is_file($file)) { require_once($file); } } } spl_autoload_register(array('Loader', 'loadClass')); //spl_autoload_register(array(__CLASS__, 'loadClass')); //spl_autoload_register(array($this, 'loadClass')); $a = new A();
更多示例
autoload.php
<?php define('SDK_PATH', __DIR__); require_once SDK_PATH . '/common/functions.php'; require_once SDK_PATH . '/common/config.php'; spl_autoload_register(function ($class) { if (false !== stripos($class, 'YJC\Wechat')) { require_once __DIR__ . '/' . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 10)) . '.php'; } }); /* function __autoload($class){ if (false !== stripos($class, 'YJC\Wechat')) { require_once __DIR__ . '/' . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 10)) . '.php'; } } */
建议入口文件里养成定义绝对地址SDK_PATH的习惯,这样require不会出错。
以上就是本文的全部内容,希望对大家的学习有所帮助。
您可能感兴趣的文章:
- php基础知识:类与对象(2) 自动加载对象
- php面向对象全攻略 (十七) 自动加载类
- php自动加载的两种实现方法
- PHP的autoload自动加载机制使用说明
- PHP spl_autoload_register实现自动加载研究
- 说说PHP的autoLoad自动加载机制
- PHP autoload与spl_autoload自动加载机制的深入理解
- PHP管理依赖(dependency)关系工具 Composer的自动加载(autoload)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
