太空船操作符 <=>

太空船操作符用于比较两个表达式
例如 当$a小于、等于或大于$b时分别返回 -1、0、1

1
2
3
echo 1<=>1;  // 0
echo 1<=>2; // -1
echo 2<=>1; // 1

类型声明

1
2
3
4
5
declare(strict_types = 1); //strict_types = 1表示开启严格模式

function sumOfInts(int ...$ints):int{
return array_sum($ints);
}

null合并操作符

1
2
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$page = isset($_GET['page']) ?? 0;

常量数组

1
define('ANIMALS',['dog','cat','bird']); // 不可修改

namespace 批量导入

1
use Space\{ClassA, ClassB, ClassC as C};

throwable接口

1
2
3
4
5
6
7
8
9
10
11
12
13
try{
undefindfunc();
} catch (Error $e){
var_dump($e);
}


set_exception_handler(
function($e){
var_dump($e);
}
);
undefindfunc();

Closure::call()

1
2
3
4
5
6
7
8
9
class Test{
private $num = 1;
}

$f = function(){
return $this->num + 1;
}

echo $f->call(new Test);

intdiv 函数

1
2
(int)(10/3);
intdiv(10, 3);

list 的方括号写法

1
2
3
4
$arr = [1, 2, 3];
list($a, $b, $c) = $arr;

[$a, $b, $c] = $arr;

小而巧的zval

定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
typedef union _zend_value {
zend_long lval; // 整型
double dval; // 浮点型
zend_refcounted *counted;
zend_string *str; // 字符串
zend_array *arr; // 数组
zend_object *obj; // 对象
zend_resource *res; // 资源
zend_reference *ref;
zend_ast_ref *ast;
zval *zv;
void *ptr;
zend_class_entry *ce; // 类
zend_function *func; // 函数
struct {
uint32_t w1;
uint32_t w2;
} ww;
} zend_value;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
union {
struct {
ZEND_ENDIAN_LOHI_4(
zend_uchar type, /* active type */
zend_uchar type_flags,
zend_uchar const_flags,
zend_uchar reserved) /* call info for EX(This) */
} v;
uint32_t type_info;
} u1;

union {
uint32_t next; /* hash collision chain */
uint32_t cache_slot; /* literal cache slot */
uint32_t lineno; /* line number (for ast nodes) */
uint32_t num_args; /* arguments number for EX(This) */
uint32_t fe_pos; /* foreach position */
uint32_t fe_iter_idx; /* foreach iterator index */
uint32_t access_flags; /* class constant access flags */
uint32_t property_guard; /* single property guard */
} u2;

php7.1 常量字符串refcount=0 type=6(zend_string) flags=2
变量字符串refcount=1 type=6(zend_string) flags=0

php7内存管理

从malloc谈起内存管理

void *ptr=malloc(size);
free(ptr); // free的时候没有传size,怎么做到准确释放size大小内存的呢?

malloc的实现

定义:

php7内存接口

void *ptr=_emalloc(size);
_efree(ptr);

基本概念

定义:

内存规格

内存预分配:使用mmap分配chunk
内存分类:
1、Small(30种规格)(size <= 3KB)
2、Large(3KB < size <= 2MB-4KB)
3、Huge(size > 2MB-4KB)

内存分配流程
定义:

small内存
定义:
定义: