PHP八大设计模式案例详解

可以更好地组织代码,与Java中的包类似。

Test1.php <?php namespace Test1;//命名空间Test1 function test(){ echo __FILE__; } Test2.php <?php namespace Test2; //命名空间Test2 function test(){ echo __FILE__;//打印当前文件所在的绝对路径。 } Test.php <?php require 'Test1.php'; require 'Test2.php'; Test1\test();//通过这种方式,使用命名空间下的方法或者类。Test1表示命名空间,test()表示该命名空间下的一个方法。 echo "<br>"; Test2\test();

运行结果

这里写图片描述

总结:通过以上代码,可以看到,在不同的命名空间下,可以有相同的类名或者方法名。

类自动载入

随着PHP项目的变大,会导致一个PHP文件的前面有很多的require去包含各种依赖的PHP文件。如果某个类删除,但是在别的文件里有导入的情况,就会导致致命错误。解决以上问题的方法,就是__autoload()函数。

Test1.php <?php class Test1{ static function test(){ echo __FILE__; } } Test2.php <?php class Test2 { static function test(){ echo __FILE__; } } Test.php <?php Test1::test(); Test2::test(); function __autoload($class){ $dir = __DIR__; $requireFile = $dir."\\".$class.".php"; require $requireFile; }

PHP就是用这段代码,去动态的载入需要包含的文件。当使用某个类,而这个类没有包含到文件中时,就会调用__autoload()函数,去动态的加载这个文件。但是,当使用多个框架时,每个框架都会有自己的__autoload()实现,所以,会导致文件重复导入。

<?php spl_autoload_register('autoload1'); spl_autoload_register('autoload2'); //将实现自动导入的函数,以字符串的形式传入该函数中,即可解决重复导入文件导致的错误问题。 Test1::test(); Test2::test(); function autoload1($class){ $dir = __DIR__; $requireFile = $dir."\\".$class.".php"; require $requireFile; } function autoload2($class){ $dir = __DIR__; $requireFile = $dir."\\".$class.".php"; require $requireFile; }

PSR-0

PHP的命名空间必须与绝对路径一致。

类名首字母大写。

除了入口文件之外,其他的PHP文件必须是一个类,不能有执行的代码。

设计模式

单例模式解决的是如何在整个项目中创建唯一对象实例的问题,工厂模式解决的是如何不通过new建立实例对象的方法。

单例模式

$_instance必须声明为静态的私有变量

构造函数和析构函数必须声明为私有,防止外部程序new 类从而失去单例模式的意义

getInstance()方法必须设置为公有的,必须调用此方法 以返回实例的一个引用

::操作符只能访问静态变量和静态函数

new对象都会消耗内存

使用场景:最常用的地方是数据库连接。

使用单例模式生成一个对象后, 该对象可以被其它众多对象所使用。

私有的__clone()方法防止克隆对象

单例模式,使某个类的对象仅允许创建一个。构造函数private修饰,
申明一个static getInstance方法,在该方法里创建该对象的实例。如果该实例已经存在,则不创建。比如只需要创建一个数据库连接。

工厂模式

工厂模式,工厂方法或者类生成对象,而不是在代码中直接new。
使用工厂模式,可以避免当改变某个类的名字或者方法之后,在调用这个类的所有的代码中都修改它的名字或者参数。

Test1.php <?php class Test1{ static function test(){ echo __FILE__; } } Factory.php <?php class Factory{ /* * 如果某个类在很多的文件中都new ClassName(),那么万一这个类的名字 * 发生变更或者参数发生变化,如果不使用工厂模式,就需要修改每一个PHP * 代码,使用了工厂模式之后,只需要修改工厂类或者方法就可以了。 */ static function createDatabase(){ $test = new Test1(); return $test; } } Test.php <?php spl_autoload_register('autoload1'); $test = Factory::createDatabase(); $test->test(); function autoload1($class){ $dir = __DIR__; $requireFile = $dir."\\".$class.".php"; require $requireFile; }

这里写图片描述

Test1.php <?php class Test1{ protected static $tt; private function __construct(){} static function getInstance(){ if(self::$tt){ echo "对象已经创建<br>"; return self::$tt; }else { self::$tt = new Test1(); echo "创建对象<br>"; return self::$tt; } } function echoHello(){ echo "Hello<br>"; } } Test.php <?php spl_autoload_register('autoload1'); $test = Test1::getInstance(); $test->echoHello(); $test = Test1::getInstance(); $test->echoHello(); $test = Test1::getInstance(); $test->echoHello(); $test = Test1::getInstance(); $test->echoHello(); function autoload1($class){ $dir = __DIR__; $requireFile = $dir."\\".$class.".php"; require $requireFile; }

注册模式

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyfdss.html