一、类的自动载入
//类的自动载入我们使用 spl_autoload_register($autoload_function )。我们需要在不同的地方包含更多不同的类文件,只需要多写几个 $autoload_function就可以了。
//例如:
spl_autoload_register('my_autoloader');
function my_autoloader($class) {
include 'classes/' . $class . '.class.php';
}
假如程序现在需要包含 Database.class.php, 当程序运行到 my_autoloadder ()时,发现Database.class.php类不存在,就会自动包含进来。
二、PSR-0规范
三、SPL库的使用 (Standard Php Library)
1、SPL常用数据结构。
SplStack (栈)
特点:只有一个出入口,跟试管儿一样,先进后出。
$stack = new SplStack();
$stack ->push('orange');
$stack ->push('banana');
$stack->rewind();
while($stack->valid()){ //对象的valid()方法检查是否到了结尾处。
var_dump($stack->current());
$stack->next();
}
//输出结果
string 'banana' (length=6)
string 'orange' (length=6)
SplQueue(队列)
特点:先进先出,后进后出
$queue = new SplQueue();
$queue->enqueue('A');
$queue->enqueue('B');
$queue->rewind();
while($queue->valid()){
echo $queue->current(),"
";
$queue->next();
}
//输出结果:
//A
//B
//注:
$queue->dequeue(); //用于删除最前面的一个数据。
SplHeap(j堆,又叫二叉堆)
特点:采用二叉树的方式存储数据。
分类:最大堆(大根堆)和最小堆(小根堆)
最大堆:根结点的键值是所有堆结点键值中最大者,且每个结点的值都比其孩子的值大。
最小堆:根结点的键值是所有堆结点键值中最小者,且每个结点的值都比其孩子的值小
下面以最小堆来进行说明
$h = new SplMinHeap();
// [parent, child]
$h->insert([9, 11]);
$h->insert([0, 1]);
$h->insert([1, 2]);
$h->insert([1, 3]);
$h->insert([1, 4]);
$h->insert([1, 5]);
$h->insert([3, 6]);
$h->insert([2, 7]);
$h->insert([3, 8]);
$h->insert([5, 9]);
$h->insert([9, 10]);
for ($h->top(); $h->valid(); $h->next()) {
list($parentId, $myId) = $h->current();
echo "$myId ($parentId)
";
}
//输出结果
1 (0)
2 (1)
3 (1)
4 (1)
5 (1)
7 (2)
6 (3)
8 (3)
9 (5)
10 (9)
11 (9)
SplFixedArray (固定长度的数组)
// Initialize the array with a fixed length
$array = new SplFixedArray(5);
$array[1] = 2;
$array[4] = "foo";
var_dump($array[0]); // NULL
var_dump($array[1]); // int(2)
var_dump($array["4"]); // string(3) "foo"
四、链式操作
链式操作最核心的一点就是要返回 $this
class DataBase
{
public function where($condition)
{
//....
return $this;
}
public function orderBy($order)
{
//....
return $this;
}
public function limit($limit)
{
//....
return $this;
}
}
操作方式:
$db = new DataBase()
$db->where()->orderBy()->limit();
例如下面这个是yii2上吗的where方法,return $this 是关键