Table of Contents
spl_autoload_register() and __autoload(), splregisterautoload
Home Backend Development PHP Tutorial spl_autoload_register() and __autoload(), splregisterautoload_PHP tutorial

spl_autoload_register() and __autoload(), splregisterautoload_PHP tutorial

Jul 12, 2016 am 08:54 AM
autoload spl and

spl_autoload_register() and __autoload(), splregisterautoload

About spl_autoload_register() and __autoload()

Look at the usage of both:

//__autoload usage function __autoload($classname) { $filename = "./class/".$classname.".class.php"; if (is_file($filename)) { include $filename; } }
/ /spl_autoload_register usage spl_autoload_register('load_class');
function load_class($classname) { $filename = "./class/".$classname.".class.php"; if (is_file($filename)) { include $filename; } }

The benefits of using spl_autoload_register() are indescribable: (1) Automatically loading objects is more convenient, and many frameworks do this:

class ClassAutoloader { public function __construct() { spl_autoload_register(array($this, 'loader')); } private function loader($className) { echo 'Trying to load ', $className, ' via ', __METHOD__, "() n"; include $className . '.php'; } }
$autoloader = new ClassAutoloader();
$obj = new Class1(); $obj = new Class2();

(2) You need to know that the __autoload() function can only exist once. Of course, spl_autoload_register() can register multiple functions

function a () { include 'a.php'; } function b () { include 'b.php'; } spl_autoload_register('a'); spl_autoload_register('b');

(3) SPL functions are rich and provide more functions, such as spl_autoload_unregister() to unregister registered functions, spl_autoload_functions() to return all registered functions, etc.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1120378.htmlTechArticlespl_autoload_register() and __autoload(), splregisterautoload About spl_autoload_register() and __autoload(), see how they are used. : //__autoload usage function __autoload($classname) {...
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 Article

Hot Article

Hot Article Tags

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)

ASUS motherboard options compatible with R55600 (including R55600u and 5600h) ASUS motherboard options compatible with R55600 (including R55600u and 5600h) Jan 02, 2024 pm 05:32 PM

ASUS motherboard options compatible with R55600 (including R55600u and 5600h)

Which one is better, Celeron g4900 or i36100? (Which one is better, Celeron g4900 or i34170?) Which one is better, Celeron g4900 or i36100? (Which one is better, Celeron g4900 or i34170?) Jan 01, 2024 pm 06:01 PM

Which one is better, Celeron g4900 or i36100? (Which one is better, Celeron g4900 or i34170?)

How to turn off the mouse and keyboard lights after turning off the Gigabyte motherboard GA78LMTS2? (The mouse light cannot be turned off after the Gigabyte a88 motherboard is shut down) How to turn off the mouse and keyboard lights after turning off the Gigabyte motherboard GA78LMTS2? (The mouse light cannot be turned off after the Gigabyte a88 motherboard is shut down) Dec 26, 2023 pm 07:58 PM

How to turn off the mouse and keyboard lights after turning off the Gigabyte motherboard GA78LMTS2? (The mouse light cannot be turned off after the Gigabyte a88 motherboard is shut down)

Which one is better, Huntkey s980 or Bauhaus (Huntkey Apollo or Lianli Bauhaus o11) Which one is better, Huntkey s980 or Bauhaus (Huntkey Apollo or Lianli Bauhaus o11) Jan 12, 2024 am 11:06 AM

Which one is better, Huntkey s980 or Bauhaus (Huntkey Apollo or Lianli Bauhaus o11)

Find maximum sum of two array elements in Java Find maximum sum of two array elements in Java Sep 11, 2023 pm 02:13 PM

Find maximum sum of two array elements in Java

Tokens to be listed: $DOGEL, $BRAIN, $BTD, $PINU and $PIMEME Tokens to be listed: $DOGEL, $BRAIN, $BTD, $PINU and $PIMEME Mar 03, 2025 pm 08:03 PM

Tokens to be listed: $DOGEL, $BRAIN, $BTD, $PINU and $PIMEME

PHP SPL data structures: Inject speed and flexibility into your projects PHP SPL data structures: Inject speed and flexibility into your projects Feb 19, 2024 pm 11:00 PM

PHP SPL data structures: Inject speed and flexibility into your projects

PHP SPL data structures: a toolkit to give your code a new look PHP SPL data structures: a toolkit to give your code a new look Feb 19, 2024 pm 12:09 PM

PHP SPL data structures: a toolkit to give your code a new look

See all articles