Home Backend Development PHP Tutorial PHP函数__autoload()和spl_autoload_register()

PHP函数__autoload()和spl_autoload_register()

Jun 23, 2016 pm 02:31 PM

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(); 
?>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

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

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

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

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

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

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

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�...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

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.

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

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...

See all articles