Heim  >  Artikel  >  Backend-Entwicklung  >  PHP实现懒加载的方法_PHP教程

PHP实现懒加载的方法_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:05:17874Durchsuche

PHP实现懒加载的方法

 这篇文章主要介绍了PHP实现懒加载的方法,实例分析了php加载的原理与懒加载的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下

 

 

本文实例讲述了PHP实现懒加载的方法。分享给大家供大家参考。具体分析如下:

寻常php的加载是通过include(),require()等方法来加载外部文件,之后再通过实例调用方法或直接调用静态方法,而这样子写引入语句实在很麻烦,有的框架会将特定路径的文件全部引入,直接实例化就能使用,但这样一来有的类包不一定用到,写的类包越多的时候,加载的东西就不少了,影响程序的性能。

通过PHP的反射类 ReflectionClass 可以直接获得对应类的一个反射类:

test.php文件如下:

?

1

2

3

4

5

6

7

class test{

public function showName(){

var_dump(__CLASS__);

}

}

?>

index.php文件如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

var_dump(get_included_files());

$rf = new ReflectionClass('test');

var_dump(get_included_files());

$testObj = $rf->newInstance();

$testObj->showName();

function __autoload($classname){

$classpath = './' . $classname . '.php';

if (file_exists($classpath)) {

require_once($classpath);

}else {

echo 'class file'.$classpath.'not found!';

}

}

?>

//array

// 0 => string 'D:\code\www\test\index.php'(length=26)

//array

// 0 => string 'D:\code\www\test\index.php'(length=26)

// 1 => string 'D:\code\www\text\test.php'(length=25)

//string 'test' (length=4)

实例化一个 ReflectionClass,并传类名进去,就会得到一个对应类的反射类。用实例调用 newInstance就会得到反射类的实例,这样就完成了实例化。

通过 get_included_files() 方法,我们可以看到当前页面引入的文件。在实例化反射类前,只有index.php文件,实例化反射类后,自动引入了一个test.php文件,那么看下上面那段代码,发现有个__autoload()名字的魔术方法,这方法就定义了自动加载文件,而ReflectionClass在当前页面找不到类时,就会调用__autoload()去加载类。这就是自动加载的过程。

想知道__autoload()方法有没有开启,可以通过PHP的标准库SPL中的方法来查看:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

var_dump(spl_autoload_functions());

spl_autoload_register('newAutoload');

var_dump(spl_autoload_functions());

$testObj1 = getInstance('test');

$testObj2 = getInstance('test');

$testObj3 = getInstance('test');

function getInstance($class, $returnInstance = false){

$rf = new ReflectionClass($class);

if ($returnInstance)

return $rf->newInstance();

}

function newAutoload($classname){

$classpath = './' . $classname . '.php';

if (file_exists($classpath)) {

var_dump('require success');

require_once($classpath);

} else {

echo 'class file ' . $classpath . ' not found!';

}

}

//array

// 0 => string '__autoload' (length=10)

//array

// 0 => string 'newAutoload' (length=11)

//string 'require success' (length=15)

sql_autoload_functions() 方法是用来查看当前自动加载的方法,当前有个__autoload魔术方法,所以返回了函数名,若没定义自动加载方法的话,返回的是false,而 spl_autoload_register() 方法是通过方法名将一个方法注册到自动加载方法,这里用newAutoload方法来替换__autoload方法。

newAutoload方法中,每执行成功一次,打印一句'require success',这里只打印了一次,说明了虽然实例了3次ReflectionClass('test'),但因为test类已经加载过一次,就不会再执行自动加载的方法。通过getInstance()这种加载类的方法,比以前的include()之类的方便多了,只需要加载这个写了getInstance()方法的文件就可以了。

重写的自动加载方法可以根据需要,通过判断类的名字,定义不同的文件路径。getInstance可以用静态变量保存实例,这也是使用到了设计模式中的单例模式。

希望本文所述对大家的php程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/963972.htmlTechArticlePHP实现懒加载的方法 这篇文章主要介绍了PHP实现懒加载的方法,实例分析了php加载的原理与懒加载的实现技巧,具有一定参考借鉴价值,需要的...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn