Home  >  Article  >  Backend Development  >  How to implement empty, isset functions in PHP kernel

How to implement empty, isset functions in PHP kernel

巴扎黑
巴扎黑Original
2016-11-08 09:37:151266browse

$TOC$

#### A few words

Originally this question was asked on oschina:


But I never received a suitable answer, so I worked hard to sort it out myself. If there are any mistakes place, welcome to communicate.

Usual functions are implemented through macro definitions such as ZEND_FUNCTION(xxx). This specification is easy to understand and the source code is easy to read.

But the processing of empty() and isset() is rather special, similar to echo, eval, etc.

#### Preparation

Extended vld for viewing PHP opcode, download:


PHP source code, branch => remotes/origin/PHP-5.6.14

git clone http://git. php.net/repository/php-src.git -b PHP-5.6.14

PHP opcode corresponding reference:


> PHP executor version is 5.6.14, other versions of opcode may have subtle differences.

PHP kernel source code analysis:


#### Start analyzing

sample code vld.php:

$a = 0;
empty($a);
isset($a);

View opcode through vld, `php -d vld.active=1 vld.php`

number of ops: 10
compiled vars: !0 = $a
line #* E I O op ext return operands
--- -------------------------------------------------- --------------------------------
2 0 E > EXT_STMT
1 ASSIGN                                   EXT_STMT
3 isset_isempty_var 293601280 ~ 1! 0
4 Free ~ 1
4 5 EXT_STMT
6 103788496 ~ 2! 0
7 free ~ 2
6 8 EXT_STMT
3

Line: 2-6 ; sop: 0; eop: 9; out1: -2

ZEND_ISSET_ISEMPTY_VAR appears in opcode, let’s analyze it step by step.

When executing the PHP source code, syntax analysis will be performed first. The yacc of empty and isset is as follows:

vim Zend/zend_language_parser.y +1265

1265 internal_functions_in_yacc:
1266 › › T_ISSET '(' isset_variables ')' { $$ = $3; }
1267 › |› T_EMPTY '(' variable ')'› { zend_do_isset_or_isempty(ZEND_ISEMPTY, &$$, &$3 TSRMLS_CC); }
1275
1276 isset_variables: 
1277 ›   ›   isset_variable› ›   ›   { $$ = $1; } 
1280 
1281 isset_variable: 
1282 ›   ›   variable›   ›   ›   ›   { zend_do_isset_or_isempty(ZEND_ISSET, &$$, &$1 TSRMLS_CC); } 

最终都执行了zend_do_isset_or_isempty,继续查找: 

git grep -in "zend_do_isset_or_isempty" 
Zend/zend_compile.c:6287:void zend_do_isset_or_isempty(int type, znode *result, znode *variable TSRMLS_DC) /* {:{:{ */ 

vi Zend/zend_compile.c +6287 

6287 void zend_do_isset_or_isempty(int type, znode *result, znode *variable TSRMLS_DC) /* {{{ */ 
6288 { 
6289 ›   zend_op *last_op; 
6290 
6291 ›   zend_do_end_variable_parse(variable, BP_VAR_IS, 0 TSRMLS_CC); 
6292 
6293 ›   if (zend_is_function_or_method_call(variable)) { 
6294 ›   ›   if (type == ZEND_ISEMPTY) { 
6295 ›   ›   ›   /* empty(func()) can be transformed to !func() */ 
6296 ›   ›   ›   zend_do_unary_op(ZEND_BOOL_NOT, result, variable TSRMLS_CC); 
6297 ›   ›   } else { 
6298 ›   ›   ›   zend_error_noreturn(E_COMPILE_ERROR, "Cannot use isset() on the result of a function call (you can use "null !== func()" instead)"); 
6299 ›   ›   } 
6300 
6301 ›   ›   return; 
6302 ›   } 
6303 
6304 ›   if (variable->op_type == IS_CV) { 
6305 ›   ›   last_op = get_next_op(CG(active_op_array) TSRMLS_CC); 
6306 ›   ›   last_op->opcode = ZEND_ISSET_ISEMPTY_VAR; 

最后一行 6306,ZEND_ISSET_ISEMPTY_VAR 这个opcode 出来了,IS_CV 判断参数是否为变量。 
注意zend_is_function_or_method_call(variable),当isset(fun($a)),函数参数写法会报错,empty在5.5版本开始支持函数参数,低版本不支持。 

opcode 是由 zend_execute 执行的,最终会对应处理函数的查找,这个是核心,请参阅:  
 

opcode 对应处理函数的命名规律: 

ZEND_[opcode]_SPEC_(变量类型1)_(变量类型2)_HANDLER 

变量类型1和变量类型2是可选的,如果同时存在,那就是左值和右值,归纳有下几类: VAR TMP CV UNUSED CONST 这样可以根据相关的执行场景来判定。 

所以 ZEND_ISSET_ISEMPTY_VAR 对应的handler如下: 

Zend/zend_vm_execute.h:44233:   ZEND_ISSET_ISEMPTY_VAR_SPEC_CONST_CONST_HANDLER, 
Zend/zend_vm_execute.h:44235:   ZEND_ISSET_ISEMPTY_VAR_SPEC_CONST_VAR_HANDLER, 
Zend/zend_vm_execute.h:44236:   ZEND_ISSET_ISEMPTY_VAR_SPEC_CONST_UNUSED_HANDLER, 
Zend/zend_vm_execute.h:44238:   ZEND_ISSET_ISEMPTY_VAR_SPEC_TMP_CONST_HANDLER, 
Zend/zend_vm_execute.h:44240:   ZEND_ISSET_ISEMPTY_VAR_SPEC_TMP_VAR_HANDLER, 
Zend/zend_vm_execute.h:44241:   ZEND_ISSET_ISEMPTY_VAR_SPEC_TMP_UNUSED_HANDLER, 
Zend/zend_vm_execute.h:44243:   ZEND_ISSET_ISEMPTY_VAR_SPEC_VAR_CONST_HANDLER, 
Zend/zend_vm_execute.h:44245:   ZEND_ISSET_ISEMPTY_VAR_SPEC_VAR_VAR_HANDLER, 
Zend/zend_vm_execute.h:44246:   ZEND_ISSET_ISEMPTY_VAR_SPEC_VAR_UNUSED_HANDLER, 
Zend/zend_vm_execute.h:44253:   ZEND_ISSET_ISEMPTY_VAR_SPEC_CV_CONST_HANDLER, 
Zend/zend_vm_execute.h:44255:   ZEND_ISSET_ISEMPTY_VAR_SPEC_CV_VAR_HANDLER, 
Zend/zend_vm_execute.h:44256:   ZEND_ISSET_ISEMPTY_VAR_SPEC_CV_UNUSED_HANDLER, 

我们看下 ZEND_ISSET_ISEMPTY_VAR_SPEC_CV_VAR_HANDLER 这个处理函数: 
vim Zend/zend_vm_execute.h +37946 

38013 ›   if (opline->extended_value & ZEND_ISSET) { 
38014 ›   ›   if (isset && Z_TYPE_PP(value) != IS_NULL) { 
38015 ›   ›   ›   ZVAL_BOOL(&EX_T(opline->result.var).tmp_var, 1); 
38016 ›   ›   } else { 
38017 ›   ›   ›   ZVAL_BOOL(&EX_T(opline->result.var).tmp_var, 0); 
38018 ›   ›   } 
38019 ›   } else /* if (opline->extended_value & ZEND_ISEMPTY) */ { 
38020 ›   ›   if (!isset || !i_zend_is_true(*value)) { 
38021 › › › ZVAL_BOOL(&EX_T(opline->result.var).tmp_var, 1);
38022 › › } else {
38023 › › › ZVAL_BOOL(&EX_T(opline->result.var).tmp_var , 0);
38024 › › }

The if ... else above is to judge whether it is isset or empty, and then do different processing, Z_TYPE_PP, i_zend_is_true are different judgments.
Echo and other processing are similar, you can analyze them in detail according to the process. The key is to find the corresponding handler processing function according to the mapping table.

After understanding these processing flows, I believe you will be more familiar with the performance analysis of PHP statements.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn