大话PHP设计模式笔记
针对PHP的设计模式进行总结记录。
顺带,我会在后面把我整理的一整套CSS3,PHP,MYSQL的开发的笔记打包放到百度云,有需要可以直接去百度云下载,这样以后你们开发就可以直接翻笔记不用百度搜那么麻烦了。
笔记链接:http://pan.baidu.com/s/1qYdQdKK 密码:pvj2
一、关于命名空间
命名空间
1.命名空间介绍
2.命名空间使用
实例:
test1.php文件代码:
1
2
3
4
5
6
7
|
<?php namespace Test1;
function test(){
echo __FILE__ ;
} ?> |
test2.php文件代码:
1
2
3
4
5
6
|
<?php namespace Test2;
function test(){
echo __FILE__ ;
} ?> |
test.php文件代码:
1
2
3
4
5
6
7
8
9
|
<?php // 如果没有 namespace命名空间 就会出现冲突 require 'test1.php' ;
require 'test2.php' ;
// 这样输出 Test1 est(); Test2 est(); ?> |
二、类的自动载入
test.php文件代码:
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
26
27
|
<?php // 这样输出 Test1::test(); Test2::test(); //下面这个函数允许有多个文件加载 ②新方法 这种比第一种先进 spl_autoload_register( 'autoload1' );
spl_autoload_register( 'autoload2' );
spl_autoload_register( 'autoload3' );
function autoload1( $class ){
require __DIR__. '/' . $class . 'php' ;
} function autoload2( $class ){
require __DIR__. '/' . $class . 'php' ;
} function autoload3( $class ){
require __DIR__. '/' . $class . 'php' ;
} ………… /* //①原来的旧方法 下面这个函数后面被抛弃了,因为多次引入相同文件会冲突 function __autoload($class){ require __DIR__.'/'.$class.'php'; } */ ?> |
三、开发一个PSR-0的基础框架
PSR-0规范
1.命名空间必须与绝对路径一致
2.类名首字母必须大写
3.除入口文件外,其他".php"必须只有一个类
开发符合PSR-0规范的基础框架
1.全部使用命名空间
2.所有PHP文件必须自动载入,不能有include/require
3.单一入口
四、SPL标准库简介
1.几种数据结构介绍:
实例:
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
26
27
28
29
30
31
32
33
34
35
36
|
<?php /* 栈结构 后进先出 $stack = new SplStack(); $stack->push("data1<br>"); $stack->push("data2<br>"); echo $stack->pop(); echo $stack->pop(); */ /* 队列 先进先出 $queue = new SplQueue(); $queue->enqueue("data1<br>"); $queue->enqueue("data2<br>"); echo $queue->dequeue(); echo $queue->dequeue(); */ /* 堆 $heap = new SplMinHeap(); $heap->insert("data1<br>"); $heap->insert("data2<br>"); echo $heap->extract(); echo $heap->extract(); */ // 打印固定尺寸数组 $array = new SplFixedArray(10);
$array [0] = 123;
$array [9] = 1234;
var_dump( $array );
?> |
2.PHP链式操作的实现
$db->where()->limit()->order();
实例:
前页:
1
2
3
4
5
6
7
8
9
10
|
<?php $db = new IMoocDatabase();
$db ->where( "id=1" );
$db ->where( "name=2" );
$db ->order( "id desc" );
$db ->limit(10);
echo $db ->where( "id=1" )->order( "id desc" )->limit(10);
?> |
Database代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?php namespace IMooc;
class Database{
function where( $where ){
return $this ;
} function order( $order ){
return $this ;
} function limit( $limit ){
return $this ;
} } ?> |
笔记链接:http://pan.baidu.com/s/1qYdQdKK 密码:pvj2
我是一名热爱旅游,读书,学习的文艺程序员。 不甘于平庸,不甘于舒适,愿不断挑战经历不同的美好。 如果您和我有共同的爱好,本人热爱交友,可以联系我。 QQ:330168885