>  기사  >  백엔드 개발  >  php中的require到底是函数还是语法

php中的require到底是函数还是语法

WBOY
WBOY원래의
2016-06-06 20:51:041245검색

我发现两种用法都可以require('functions.php')require 'functions.php',那么它在php中到底是语法还是函数呢?我们能否在自己写php插件时也实现这样的用法?也就是可以这样用my_custom 'parms'

回复内容:

我发现两种用法都可以require('functions.php')require 'functions.php',那么它在php中到底是语法还是函数呢?我们能否在自己写php插件时也实现这样的用法?也就是可以这样用my_custom 'parms'

语法
Zend/zend_language_scanner.l

<st_in_scripting>"eval" {
	return T_EVAL;
}

<st_in_scripting>"include" {
	return T_INCLUDE;
}

<st_in_scripting>"include_once" {
	return T_INCLUDE_ONCE;
}

<st_in_scripting>"require" {
	return T_REQUIRE;
}

<st_in_scripting>"require_once" {
	return T_REQUIRE_ONCE;
}</st_in_scripting></st_in_scripting></st_in_scripting></st_in_scripting></st_in_scripting>

Zend/zend_language_parser.y

internal_functions_in_yacc:
		T_ISSET '(' isset_variables ')' { $$ = $3; }
	|	T_EMPTY '(' variable ')'	{ zend_do_isset_or_isempty(ZEND_ISEMPTY, &$$, &$3 TSRMLS_CC); }
	|	T_INCLUDE expr 			{ zend_do_include_or_eval(ZEND_INCLUDE, &$$, &$2 TSRMLS_CC); }
	|	T_INCLUDE_ONCE expr 	{ zend_do_include_or_eval(ZEND_INCLUDE_ONCE, &$$, &$2 TSRMLS_CC); }
	|	T_EVAL '(' expr ')' 	{ zend_do_include_or_eval(ZEND_EVAL, &$$, &$3 TSRMLS_CC); }
	|	T_REQUIRE expr			{ zend_do_include_or_eval(ZEND_REQUIRE, &$$, &$2 TSRMLS_CC); }
	|	T_REQUIRE_ONCE expr		{ zend_do_include_or_eval(ZEND_REQUIRE_ONCE, &$$, &$2 TSRMLS_CC); }
;

Zend/zend_compile.c

void zend_do_include_or_eval(int type, znode *result, const znode *op1 TSRMLS_DC) 
{
	zend_do_extended_fcall_begin(TSRMLS_C);
	{
		zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);

		opline->opcode = ZEND_INCLUDE_OR_EVAL;
		opline->result_type = IS_VAR;
		opline->result.var = get_temporary_variable(CG(active_op_array));
		SET_NODE(opline->op1, op1);
		SET_UNUSED(opline->op2);
		opline->extended_value = type;
		GET_NODE(result, opline->result);
	}
	zend_do_extended_fcall_end(TSRMLS_C);
}

require是 PHP 中的关键字。

f()这样的用法并不意味着f是函数。例如,print('hi'也可以,但是 print 并非函数。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.