这篇文章主要为大家详细介绍了PHP利用spl_autoload_register与autoload的示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随512笔记的小玲来看看吧!
spl_autoload_register代码如下:
<?php
/* 512笔记 www.512Pic.com */
class A
{
public function __construct()
{
echo 'Got it.';
}
}
代码如下:
<?php
/* 512笔记 www.512Pic.com */
require('A.php');
$a = new A();
代码如下:
<?php
/* 512笔记 www.512Pic.com */
function __autoload($class)
{
$file = $class . '.php';
if (is_file($file)) {
require_once($file);
}
}
$a = new A();
代码如下:
<?php
/* 512笔记 www.512Pic.com */
function loader($class)
{
$file = $class . '.php';
if (is_file($file)) {
require_once($file);
}
}
spl_autoload_register('loader');
$a = new A();
代码如下:
<?php
/* 512笔记 www.512Pic.com */
class Loader
{
public static function loadClass($class)
{
$file = $class . '.php';
if (is_file($file)) {
require_once($file);
}
}
}
spl_autoload_register(array('Loader', 'loadClass'));
$a = new A();
本文来自:http://www.512pic.com/173/15657-0.html
注:关于PHP利用spl_autoload_register与autoload的示例的内容就先介绍到这里,更多相关文章的可以留意512笔记的其他信息。
关键词:
512笔记收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。