PHP函数__autoload()和spl_autoload_register()
PHP函数__autoload()和spl_autoload_register()
__autoload($classname){
}
当PHP找不到类文件会调用这个方法,当注册了自己的函数或方法时,PHP不会调用__autoload()函数,而会调用自定义的函数
spl_autoload_register('func_name');
spl_autoload_register(array('class_name','method_name'));
详细说明如下:
spl_autoload_register
(PHP 5 >= 5.1.2)
spl_autoload_register ? 注册__autoload()函数
说明
bool spl_autoload_register ([ callback $autoload_function ] )
将函数注册到SPL __autoload函数栈中。如果该栈中的函数尚未激活,则激活它们。
如果在你的程序中已经实现了__autoload函数,它必须显式注册到__autoload栈中。因为
spl_autoload_register()函数会将Zend Engine中的__autoload函数取代为spl_autoload()或
spl_autoload_call()。
参数
autoload_function
欲注册的自动装载函数。如果没有提供任何参数,则自动注册autoload的默认实现函数
spl_autoload()。
返回值
如果成功则返回 TRUE,失败则返回 FALSE。
注:SPL是Standard PHP Library(标准PHP库)的缩写。它是PHP5引入的一个扩展库,其主要功能包括autoload机制的实现及包括各种Iterator接口或类。SPL autoload机制的实现是通过将函数指针autoload_func指向自己实现的具有自动装载功能的函数来实现的。SPL有两个不同的函数spl_autoload, spl_autoload_call,通过将autoload_func指向这两个不同的函数地址来实现不同的自动加载机制。
test.class.php
class Test
{
function __construct()
{
echo '希望和PHP,Flex高手成为朋友。一起学习,共同奋斗!!!QQ:1055983238';
}
}
?>
load.php
class LOAD
{
static function loadClass($class_name)
{
$filename = $class_name.".class.php";
if (is_file($filename)) return include_once $filename;
}
}
/**
* 设置对象的自动载入
* spl_autoload_register ? Register given function as __autoload() implementation
*/
spl_autoload_register(array('LOAD', 'loadClass'));
$a = new Test();//实现自动加载,很多框架就用这种方法自动加载类
?>
class autoload
{
public static function load( $class name )
{
$filename = "/home/user/class/".$classname."class.php";
if (file_exists($filename )) {
require_once $filename ;
}
}
}
function __autoload( $class name )
{ // 这个是默认的 autoload 方法
$filename = "/home/user/class/".$classname."class.php";
if (file_exists($filename )) {
require_once $filename ;
}
}
// 注册一个 autoloader
spl_autoload_register( 'autoload::load' );
/**
* __autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法
* 可以通过下面的方法来把 _autoload 方法加入 autoload_functions list
*/
spl_autoload_register( '__autoload' );
// 注:下面的类看上去没有定义,但其实系统根据sql_autoload_register提供的路径会自动去/home/user// /class/*.class.php下搜索foo.class.php文件,如果没找到才报错。
$foo = new foo();
$foo ->bar();
?>

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



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...
