php设计模式 工厂、单例、注册树模式,php设计模式
Source Code Pro字体 easyphp
命名空间:隔离类和函数,php5.3以后
//test5.php<br /><?<span>php namespace Test5;<span>//命名空间必须是程序脚本的第一条语句,除了declare <span>function<span> test(){ <span>echo <span>__FILE__<span>; }</span></span></span></span></span></span></span>
//test6.php<br /><?<span>php namespace Test6; <span>function<span> test(){ <span>echo <span>__FILE__<span>; }</span></span></span></span></span></span>
<span><span><?php<br />require 'test5.php'<span>; <span>require 'test6.php'<span>; Test5\test(); Test6\test();</span></span></span></span></span>
类自动载入:php5.2以后
spl_autoload_register('autoload1'<span>); Test5::<span>test(); Test6::<span>test(); <span>function autoload1(<span>$class<span>){ <span>require __DIR__.'/'.<span>$class.'.php'<span>; }</span></span></span></span></span></span></span></span></span>
PSR-0规范:
- 命名空间必须与绝对路径一致
- 类名首字母必须大写
- 除入口文件外,其他“.php” 必须只有一个类。
开发符合PSR-0规范的基础框架
spl标准库:
PHP链式操作:
<?<span>php namespace Baobab; </span><span>class</span><span> Database{ </span><span>function</span> where(<span>$where</span><span>){ </span><span>return</span> <span>$this</span><span>; } </span><span>function</span> order(<span>$order</span><span>) { </span><span>return</span> <span>$this</span><span>; } </span><span>function</span> limit(<span>$limit</span><span>){ </span><span>return</span> <span>$this</span><span>; } } </span>?><br /><br />//index.php<br />$db = new Baobab\Database();<br />$db->where('id = 1')->order('order by id')->limit(1);
魔术方法:
- __get/__set:接管对象属性。在给不可访问属性赋值时,__set() 会被调用;读取不可访问属性的值时,__get() 会被调用。
- __call/__callStatic:在对象中调用一个不可访问方法时,__call() 会被调用;用静态方式中调用一个不可访问方法时,__callStatic() 会被调用。
- __toString:一个类转化成字符串
- __invoke:以调用函数的方式调用一个对象时,__invoke() 方法会被自动调用。
object.php
<?<span>php namespace Baobab; </span><span>class</span> <span>Object</span><span>{ </span><span>protected</span> <span>$array</span> = <span>array</span><span>(); </span><span>function</span> __set(<span>$key</span>, <span>$value</span><span>){ </span><span>$this</span>-><span>array</span>[<span>$key</span>] = <span>$value</span><span>; } </span><span>function</span> __get(<span>$key</span><span>){ </span><span>//</span><span>echo __METHOD__;</span> <span>return</span> <span>$this</span>-><span>array</span>[<span>$key</span><span>]; } </span><span>function</span> __call(<span>$func</span>, <span>$param</span><span>){ </span><span>//</span><span>var_dump($func,$param);</span> <span>return</span> 'magic function'<span>; } </span><span>static</span> <span>function</span> __callstatic(<span>$func</span>, <span>$param</span><span>) { <span>//</span></span><span>var_dump($func, $param); </span><span>return</span> 'magic static function'<span>; } </span><span>function</span><span> __toString(){ </span><span>return</span> <span>__CLASS__</span><span>; } </span><span>function</span> __invoke(<span>$param</span><span>) { </span><span>return</span> <span>var_dump</span>(<span>$param</span><span>); } }</span>
index.php
<span>$obj</span> = <span>new</span> baobab\<span>Object</span><span>(); </span><span>$obj</span>->title = 'hello'<span>; </span><span>echo</span> <span>$obj</span>-><span>title; </span><span>echo</span> <span>$obj</span>->test1('hello', 123<span>); </span><span>echo</span> <span>$obj</span>::test1('hello1', 1234<span>); </span><span>echo</span> <span>$obj</span><span>; </span><span>echo</span> <span>$obj</span>('test1');
1、三种基本设计模式
- 工厂模式:使用工厂方法或类生产对象,而不是在代码中直接new
Factory.php
<?<span>php namespace Baobab; </span><span>class</span><span> Factory{ </span><span>static</span> <span>function</span><span> createDatabase(){ </span><span>$db</span> = <span>new</span><span> Database(); </span><span>return</span> <span>$db</span><span>; } }</span>
index.php
<span>$db = Baobab\Factory::<span>createDatabase();<br /></span>$db1 = Baobab\Factory::<span>createDatabase();</span></span>
<span><span><span>$db->limit(<span>$limit);</span></span></span></span>
- 单例模式:使某个类的对象仅允许创建一个
Database.php
<?<span>php namespace Baobab; </span><span>class</span><span> Database{ </span><span>protected</span> <span>static</span> <span>$db</span><span>; </span><span>private</span> <span>function</span><span> __construct(){ } </span><span>static</span> <span>function</span><span> getInstance(){ </span><span>if</span> (self::<span>$db</span><span>){ </span><span>return</span> self::<span>$db</span><span>; }</span><span>else</span><span>{ </span><span>//</span><span>self是指向类本身,也就是self是不指向任何已经实例化的对象(::域运算符号)</span> self::<span>$db</span> = <span>new</span><span> self(); </span><span>return</span> self::<span>$db</span><span>; } }</span>
index.php
<span>$db</span> = Baobab\Database::getInstance();
- 注册模式:解决全局共享和交换对象,将对象注册到全局树上,可以在任何地方被直接访问
Register.php
<?<span>php namespace Baobab; </span><span>class</span><span> Register{ </span><span>protected</span> <span>static</span> <span>$objects</span><span>; </span><span>static</span> <span>function</span> set(<span>$alias</span>, <span>$object</span><span>){ self</span>::<span>$objects</span>[<span>$alias</span>] = <span>$object</span><span>; } </span><span>static</span> <span>function</span> _unset(<span>$alias</span><span>) { </span><span>unset</span>(self::<span>$objects</span>[<span>$alias</span><span>]); } </span><span>static</span> <span>function</span> get(<span>$name</span><span>) { </span><span>return</span> self::<span>$objects</span>[<span>$name</span><span>]; } }</span>
<span>//将db注册到注册树上</span> Register::set('db1', <span>$db</span><span>); </span>
index.php
<span>$db</span> = Baobab\Register::get('db1');

php5和php8的区别在性能、语言结构、类型系统、错误处理、异步编程、标准库函数和安全性等方面。详细介绍:1、性能提升,PHP8相对于PHP5来说在性能方面有了巨大的提升,PHP8引入了JIT编译器,可以对一些高频执行的代码进行编译和优化,从而提高运行速度;2、语言结构改进,PHP8引入了一些新的语言结构和功能,PHP8支持命名参数,允许开发者通过参数名而不是参数顺序等等。

php5改80端口的方法:1、编辑Apache服务器的配置文件中的端口号;2、辑PHP的配置文件以确保PHP在新端口上工作;3、重启Apache服务器,PHP应用程序将开始在新的端口上运行。

php5没有监听9000端口解决方法步骤:1、检查PHP-FPM配置文件;2、重启PHP-FPM服务;3、关闭防火墙或配置端口转发;4、检查其他进程是否占用9000端口。

php7和php5语法区别有:1、PHP7引入了严格的类型声明,而PHP5变量的类型是隐式的;2、PHP7引入了对标量类型声明的支持,而PHP5并没有;3、PHP7引入了NULL合并运算符,而PHP5检查一个变量是否存在并且不为null,需要使用条件语句;4、PHP7添加了新的比较运算符“<=>”,而PHP5并没有;5、PHP7引入新特性匿名类,而PHP5并没有。

php7的版本和5的不同有性能提升、标量类型声明、返回值类型声明、异常处理改进、匿名类、语法改进、新的运算符、增强的错误处理和移除了一些旧特性等。详细介绍:1、性能提升,PHP7引入了全新的Zend引擎,名为Zend Engine 3.0,带来了显著的性能提升,PHP7的性能比PHP5提高了大约两倍,这主要是通过改进了内存管理、优化了函数调用和异常处理、增强了垃圾回收等等。

在使用PHP5时,有些用户可能会遇到无法监听端口9000的情况。这时,我们需要进行一些配置和排查工作来解决这一问题。

如果你是一名网站管理员,你可能会碰到需要将 PHP5 的端口从默认的 80 端口进行修改的情况。这个过程可能会稍有进急,但是只要跟着以下步骤操作,一定会很容易地完成。

PHP是一种广泛使用的服务器端编程语言,被用于开发动态网站和应用程序。近些年,PHP7的发布引起了一些关注。PHP7比之前的版本(如PHP5)有很多改进和优化。在本文中,我们将探讨PHP7和PHP5在语法上的不同之处。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 English version
Recommended: Win version, supports code prompts!
