一、几个Array排序函数
- sort()和rsort()
对数组中的元素按字母进行升序/降序排序1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$b = array(“a”=>5,”b”=>2,”c”=>3);
sort($b);
print_r($b);
/*Array
(
[0] => 2
[1] => 3
[2] => 5
)*/
rsort($b);
print_r($b);
/*Array
(
[0] => 5
[1] => 3
[2] => 2
)*/ - ksort()和krsort()
对数组按照键名进行升序/降序排序1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$b = array(“a”=>5,”b”=>2,”c”=>3);
ksort($b);
print_r($b);
/*Array
(
[a] => 5
[b] => 2
[c] => 3
)*/
krsort($b);
print_r($b);
/*Array
(
[c] => 3
[b] => 2
[a] => 5
)*/ - asort()和arsort()
对关联数组按照键值进行升序/降序排序,同时会保持索引关系1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$b = array(“a”=>5,”b”=>2,”c”=>3);
asort($b);
print_r($b);
/*Array
(
[b] => 2
[c] => 3
[a] => 5
)*/
ksort($b);
print_r($b);
/*Array
(
[a] => 5
[b] => 2
[c] => 3
)*/
二、require()和include
- require()一般放在程序最前面,执行前会将对应文件读取进来,require没有返回值
- include()一般不强制放在程序最前面,也可以放在程序中间,做流程控制,只有当执行到这里的时候才会把对应的文件读取进来,include有返回值
- require一个文件存在错误的话,程序就会中断执行,并抛出致命错误,include一个文件存在错误的话,程序不会中断,但是会显示一个警告
- require()通常来导入静态的内容,而include()则适合用导入动态的程序代码。
- require_once()会先检查目标文件的内容是不是在之前就已经导入过了,如果是的话,便不会再次重复导入同样的内容。
三、extends()和implements()
- extends,一般是单继承某个类,继承之后可以使用父类的方法,也可以重写父类的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
abstract class Father{
public function getName(){
return "I'm your Father";
}
}
class Son extends Father{
public function getFatherName(){
echo $this->getName();
}
}
$a = new Son();
$a->getFatherName(); //I'm your Father - implements,一般是实现多个接口,接口的方法一般是空的,需要重写才能实现
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
interface iTemplate
{
public function setVariable($name, $var);
public function getHtml($template);
}
class Template implements iTemplate
{
private $vars = array();
public function setVariable($name, $var)
{
// TODO: Implement setVariable() method.
$this->vars[$name] = $var;
}
public function getHtml($template)
{
// TODO: Implement getHtml() method.
foreach ($this->vars as $name => $value){
$template = str_replace(‘{‘ . $name . ‘}’, $value, $template);
}
return $template;
}
} - 在interface之间也可以声明为extends(多继承)的关系。注意一个interface可以extends多个其他interface。
1 |
|
- 更多接口对象参考:PHP: 对象接口 - Manual
接下来研究:
- 命名空间
- 排序算法
- 设计模式