php编译原理,2-3 Opcodes编译原理

**一.opcodes作用**

opcodes是一种php脚本编译后的中间语言,就像java的bytecode,或者.net的msl

**二.原生php代码**

```

echo "Hello World";

$a = 1 + 1;

echo $a;

?>

```

**三.PHP执行这段代码会经过如下4个步骤**

```

#PHP的语言引擎Zend

1.Scanning(Lexing) ,将PHP代码转换为语言片段(Tokens)

2.Parsing, 将Tokens转换成简单而有意义的表达式

3.Compilation, 将表达式编译成Opocdes

4.Execution, 顺次执行Opcodes,每次一条,从而实现PHP脚本的功能

```

**四.Scanning**

```

#用token_get_all处理以上PHP原生代码

#index.php

$str='<?php

echo "Hello World";

$a = 1 + 1;

echo $a;

?>';

$arr=$tokens=token_get_all($str);

echo "

";

print_r($arr);

echo "

";

?>

```

```

Array

(

[0] => Array

(

[0] => 367

[1] => Array

(

[0] => 316

[1] => echo

)

[2] => Array

(

[0] => 370

[1] =>

)

[3] => Array

(

[0] => 315

[1] => "Hello World"

)

[4] => ;

[5] => Array

(

[0] => 370

[1] =>

)

[6] => =

[7] => Array

(

[0] => 370

[1] =>

)

[8] => Array

(

[0] => 305

[1] => 1

)

[9] => Array

(

[0] => 370

[1] =>

)

[10] => +

[11] => Array

(

[0] => 370

[1] =>

)

[12] => Array

(

[0] => 305

[1] => 1

)

[13] => ;

[14] => Array

(

[0] => 370

[1] =>

)

[15] => Array

(

[0] => 316

[1] => echo

)

[16] => Array

(

[0] => 370

[1] =>

)

[17] => ;

)

```

2.Parsing

#接下来,就是Parsing阶段了,Parsing首先会丢弃Tokens Array中的多余的空格,将剩下的转成一个一个的简单的表达式

```

1.echo a constant string

2.add two numbers together

3.store the result of the prior expression to a variable

4.echo a variable

```

3.Compilation

```

#它会把Tokens编译成一个个op_array

* ZEND_ECHO 'Hello World'

* ZEND_ADD ~0 1 1

* ZEND_ASSIGN !0 ~0

* ZEND_ECHO !0

> ```

4.由Zend引擎一行一行读取并执行.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值