spl_autoload_register() and __autoload(), splregisterautoload_PHP tutorial

WBOY
Release: 2016-07-12 08:54:24
Original
940 people have browsed it

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) {...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template