


spl_autoload_register() and __autoload(), splregisterautoload_PHP tutorial
Jul 12, 2016 am 08:54 AMspl_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.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

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

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

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)

Find maximum sum of two array elements in Java

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: a toolkit to give your code a new look
