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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# boolean
$bool_1 = true;
$bool_2 = false;
$bool_3 = (boolean)1; // true

# integer (只有有符号int)
# 超过最大int将会返回float类型的数字
$int_1 = -1000; // 十进制
$int_2 = 0100; // 八进制
$int_3 = 0x100; // 十六进制
$int_4 = 0b11111111; // 二进制

# float
$float_1 = 1.1;
$float_2 = 7.9E-3; // 7.9 * 10^(-3)

# string
$str_1 = 'a\'b\''.'c'; # .用于拼接字符串
$str_2 = '$a'; # 单引号中的变量不会解析,效率高
$str_3 = "$a"; # 双引号中变量正常解析
$str_4 = strval($int_1);

# 定界符
$c = <<<STR
多行文本
$str_1
这里出现的''""不用转义
STR;

# 常量
define("FOO", "true"); #🙄
echo FOO;

# array
# 索引既可以是数字也可以是字符串, array = list + dic
$arr_1 = (1 => 'one', '2' => true);
$arr_2 = [1 => 'one', '2' => true];
$arr_3 = ['1', '2', '3'];
echo arr_1[1];
print_r($arr_1); //查看数组全部内容
$arr_3[] = '4';
array_push($arr_3, '5');
unset($arr_3[2]) //删除第三个元素'3'

# foreach数组的key,这里$key并不是保留的关键字,可以为任意变量名
foreach(array_keys($arr_1) as $key)
{
echo $key . '\n';
}

# foreach数组的key和value
foreach($arr_1 as $key => $value)
{
echo $key. '=>' . $value . '\n';
}

# 匿名函数
$anonymous_func = function($args)
{
return $args + 1;
}

echo $anonymous_func(2);

function func_1($x, $y):
{
return function($args) use ($x, $y)
{
echo $args + $x + $y;
}
}

$fc = func_1(1, 2);
$fc(3); //6

# 可以通过函数名字符串来调用函数,动态调用函数
$func_name = 'func_1';
echo $func_name(1, 2);

# 向函数传不定长参数
function parameters()
{
$args_num = func_num_args(); // 参数数量
$args_array = func_get_args();
foreach($args_array as $key => $value)
{
echo $key . '-' . $value;
}
}

function func_2($word, ...$list)
{
foreach($list as $item)
{
echo $item . '\n';
}
}

# 返回变量的类型
var_dump($irobot);

//$irobot是否被定义且不为null
isset($irobot);

# <=> $a <=> $b 一次性作三种比较,返回 -1, 0, 1
$a = 1;
$b = 2;
$c = $a <=> $b; // $c = -1

# 语法糖
$d = null;
$b = $d ?? '$d is null'; //$d为null则返回后面的

面向对象

__CLASS__ 当前类名,必须在类的内部定义
__DIR__ 文件路径

#TODO
__FUNCTION__ 当前函数名??

__LINE__ 当前代码行号

#TODO
__METHOD__ 当前函数名 ??

__NAMESPACE__ 当前命名空间

__TRAIT__

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
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
class Robot{
const MY_CONST = 'const';
static $staticVar = 'static';
var $name;
public $public = 'public';
private $private = 'private';
protected $protected = 'protected';
public function go(){}

# 不用为每个字段写get,set?
public function __get($key)
{
return $this->$key;
}

public function __set($key, $value)
{
$this->$key = $value;
}

# 构造函数
public function __construct($instanceProp)
{

}

# 不可重载
final function finalFunc()
{

}

# 转成string时的方法
public function __toString()
{

}

# 析构函数
public function __destruct()
{

}
}
$irobot = new Robot;
$irobot->name = "park";
echo $irobot->name;
?>

接口

1
2
3
4
5
6
7
8
9
10
11
12
interface INotifyPropertyChanged
{
public function OnPropertyChanged();
}

class MainViewModel implements INotifyPropertyChanged
{
public function OnPropertyChanged()
{

}
}

类的继承

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
37
38
39
40
/**********************
* Late Static Binding
*
*/

class ParentClass
{
public static function who()
{
echo "I'm a " . __CLASS__ . "\n";
}

public static function test()
{
// self references the class the method is defined within
self::who();
// static references the class the method was invoked on
static::who();
}
}

ParentClass::test();
/*
I'm a ParentClass
I'm a ParentClass
*/

class ChildClass extends ParentClass
{
public static function who()
{
echo "But I'm " . __CLASS__ . "\n";
}
}

ChildClass::test();
/*
I'm a ParentClass
But I'm ChildClass
*/

trait
类似C#中的behavior

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
trait ezcReflectionReturnInfo {
function getReturnType() { /*1*/ }
function getReturnDescription() { /*2*/ }
}

class ezcReflectionMethod extends ReflectionMethod {
use ezcReflectionReturnInfo;
/* ... */
}

class ezcReflectionFunction extends ReflectionFunction {
use ezcReflectionReturnInfo;
/* ... */
}
?>

方法覆盖优先级
当前类中方法 -> trait中的方法 -> 从基类中继承的方法

网页相关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 控制网页元素,类似jsp
<?php if ($x):?>
$x
<?php else:?>
$x is null
<?php endif?>

# 包含和依赖其他文件

<?php
include 'my-file.php';
include_once 'my-file.php'; //像C中头文件的include

//文件未找到将会报错
require 'my-file.php';
require_once 'my-file.php';


>

异常处理

1
2
3
4
5
6
7
8
9
10
11
12
13
class MyException extends Exception {}

try {

$condition = true;

if ($condition) {
throw new MyException('Something just happened');
}

} catch (MyException $e) {
// Handle my exception
}

值传递和引用传递

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// 值传递
function foo($var)
{
$var++;
}
// 引用传递
function foo1(& $var)
{
&var++;
}

$a = 5;
foo($a); //5

foo1($a); //6
?>

Reference

  1. https://learnxinyminutes.com/docs/php/