php 对象的执行

1.BNF范式

%token T_OBJECT_OPERATOR "-> (T_OBJECT_OPERATOR)"

unticked_statement:
| expr ';' { zend_do_free(&$ TSRMLS_CC); } expr:
r_variable { $$ = $; }
| expr_without_variable { $$ = $; }
; r_variable:
variable { zend_do_end_variable_parse(&$, BP_VAR_R, TSRMLS_CC); $$ = $; }
;

//object_property可能是成员变量,可能是成员函数,可产生$obj->xx->xx()的形式
variable:
base_variable_with_function_calls T_OBJECT_OPERATOR { zend_do_push_object(&$ TSRMLS_CC); }
object_property { zend_do_push_object(&$ TSRMLS_CC); } method_or_not variable_properties
{ zend_do_pop_object(&$$ TSRMLS_CC); $$.EA = $.EA | ($.EA ? $.EA : $.EA); }
| base_variable_with_function_calls { $$ = $; }
;

  base_variable_with_function_calls:
    base_variable { $$ = $1; }
  ;


  base_variable:
    reference_variable { $$ = $1; $$.EA = ZEND_PARSED_VARIABLE; }
  ;


 reference_variable:
| compound_variable { zend_do_begin_variable_parse(TSRMLS_C); fetch_simple_variable(&$$, &$1, 1 TSRMLS_CC); }
; compound_variable:
T_VARIABLE { $$ = $1; }
| '$' '{' expr '}' { $$ = $3; }
;
object_property: 
  object_dim_list { $$ = $; }
;

object_dim_list:

    variable_name { znode tmp_znode; zend_do_pop_object(&tmp_znode TSRMLS_CC); zend_do_fetch_property(&$$, &tmp_znode, &$1 TSRMLS_CC);}
  ;


variable_name:
  T_STRING { $$ = $1; }
  ;

  

 

 variable_properties:
   variable_properties variable_property { $$.EA = $2.EA; }
   | /* empty */ { $$.EA = 0; }
   ;

 variable_property:
    T_OBJECT_OPERATOR object_property { zend_do_push_object(&$2 TSRMLS_CC); } method_or_not { $$.EA = $4.EA; }
  ;

method_or_not:
method { $$ = $; $$.EA = ZEND_PARSED_METHOD_CALL; zend_do_push_object(&$$ TSRMLS_CC); }
| array_method_dereference { $$ = $; zend_do_push_object(&$$ TSRMLS_CC); }
| /* empty */ { $$.EA = ZEND_PARSED_MEMBER; }
; method:
'(' { zend_do_pop_object(&$ TSRMLS_CC); zend_do_begin_method_call(&$ TSRMLS_CC); }
function_call_parameter_list ')'
{ zend_do_end_function_call(&$, &$$, &$, , TSRMLS_CC); zend_do_extended_fcall_end(TSRMLS_C); }
;

1对于$this->abc中的$this

void fetch_simple_variable_ex(znode *result, znode *varname, int bp, zend_uchar op TSRMLS_DC) /* {{{ */
{
zend_op opline;
zend_op *opline_ptr;
zend_llist *fetch_list_ptr; if (varname->op_type == IS_CONST) {
ulong hash; //如果不是以$this开头的变量,根据lookup_cv函数,在active_syymble_table变量中增加一个key
hash = str_hash(Z_STRVAL(varname->u.constant), Z_STRLEN(varname->u.constant));
if (!zend_is_auto_global_quick(Z_STRVAL(varname->u.constant), Z_STRLEN(varname->u.constant), hash TSRMLS_CC) &&
!(Z_STRLEN(varname->u.constant) == (sizeof("this")-) &&
!memcmp(Z_STRVAL(varname->u.constant), "this", sizeof("this") - )) &&
(CG(active_op_array)->last == ||
CG(active_op_array)->opcodes[CG(active_op_array)->last-].opcode != ZEND_BEGIN_SILENCE)) {
result->op_type = IS_CV;
result->u.op.var = lookup_cv(CG(active_op_array), Z_STRVAL(varname->u.constant), Z_STRLEN(varname->u.constant), hash TSRMLS_CC);
Z_STRVAL(varname->u.constant) = (char*)CG(active_op_array)->vars[result->u.op.var].name;
result->EA = ;
return;
}
} if (bp) {
opline_ptr = &opline;
init_op(opline_ptr TSRMLS_CC);
} else {
opline_ptr = get_next_op(CG(active_op_array) TSRMLS_CC);
} opline_ptr->opcode = op;
opline_ptr->result_type = IS_VAR;
opline_ptr->result.var = get_temporary_variable(CG(active_op_array));
SET_NODE(opline_ptr->op1, varname);
GET_NODE(result, opline_ptr->result);
SET_UNUSED(opline_ptr->op2);
opline_ptr->extended_value = ZEND_FETCH_LOCAL; if (varname->op_type == IS_CONST) {
CALCULATE_LITERAL_HASH(opline_ptr->op1.constant);
if (zend_is_auto_global_quick(Z_STRVAL(varname->u.constant), Z_STRLEN(varname->u.constant), Z_HASH_P(&CONSTANT(opline_ptr->op1.constant)) TSRMLS_CC)) {
opline_ptr->extended_value = ZEND_FETCH_GLOBAL;
}
} if (bp) {
zend_stack_top(&CG(bp_stack), (void **) &fetch_list_ptr);
zend_llist_add_element(fetch_list_ptr, opline_ptr);
}
}
/* }}} */ void fetch_simple_variable(znode *result, znode *varname, int bp TSRMLS_DC) /* {{{ */
{
/* the default mode must be Write, since fetch_simple_variable() is used to define function arguments */
fetch_simple_variable_ex(result, varname, bp, ZEND_FETCH_W TSRMLS_CC);
}

zend_do_fetch_property

void zend_do_fetch_property(znode *result, znode *object, const znode *property TSRMLS_DC) /* {{{ */
{
zend_op opline;
zend_llist *fetch_list_ptr; zend_stack_top(&CG(bp_stack), (void **) &fetch_list_ptr); if (object->op_type == IS_CV) {
if (object->u.op.var == CG(active_op_array)->this_var) {
object->op_type = IS_UNUSED; /* this means $this for objects */
}
} else if (fetch_list_ptr->count == ) {
//针对于$abc=1;或$this->abc=1;的这种情况
//对$abc或$this已经进行了fetch_simple_variable函数处理了
zend_llist_element *le = fetch_list_ptr->head;
zend_op *opline_ptr = (zend_op *) le->data; if (opline_is_fetch_this(opline_ptr TSRMLS_CC)) {
zend_del_literal(CG(active_op_array), opline_ptr->op1.constant);
SET_UNUSED(opline_ptr->op1); /* this means $this for objects */
SET_NODE(opline_ptr->op2, property);
/* if it was usual fetch, we change it to object fetch */
switch (opline_ptr->opcode) {
case ZEND_FETCH_W:
opline_ptr->opcode = ZEND_FETCH_OBJ_W;
break;
case ZEND_FETCH_R:
opline_ptr->opcode = ZEND_FETCH_OBJ_R;
break;
}
if (opline_ptr->op2_type == IS_CONST && Z_TYPE(CONSTANT(opline_ptr->op2.constant)) == IS_STRING) {
CALCULATE_LITERAL_HASH(opline_ptr->op2.constant);
GET_POLYMORPHIC_CACHE_SLOT(opline_ptr->op2.constant);
}
GET_NODE(result, opline_ptr->result);
return;
}
} if (zend_is_function_or_method_call(object)) {
init_op(&opline TSRMLS_CC);
opline.opcode = ZEND_SEPARATE;
SET_NODE(opline.op1, object);
SET_UNUSED(opline.op2);
opline.result_type = IS_VAR;
opline.result.var = opline.op1.var;
zend_llist_add_element(fetch_list_ptr, &opline);
} init_op(&opline TSRMLS_CC);
opline.opcode = ZEND_FETCH_OBJ_W; /* the backpatching routine assumes W */
opline.result_type = IS_VAR;
opline.result.var = get_temporary_variable(CG(active_op_array));
SET_NODE(opline.op1, object);
SET_NODE(opline.op2, property);
if (opline.op2_type == IS_CONST && Z_TYPE(CONSTANT(opline.op2.constant)) == IS_STRING) {
CALCULATE_LITERAL_HASH(opline.op2.constant);
GET_POLYMORPHIC_CACHE_SLOT(opline.op2.constant);
}
GET_NODE(result, opline.result); zend_llist_add_element(fetch_list_ptr, &opline);
}

2函数 zend_do_begin_method_call

void zend_do_begin_method_call(znode *left_bracket TSRMLS_DC) /* {{{ */
{
zend_op *last_op;
int last_op_number;
unsigned char *ptr = NULL;
//代码省略
last_op_number = get_next_op_number(CG(active_op_array))-; //$obj->func()
last_op = &CG(active_op_array)->opcodes[last_op_number];
//代码省略
if (last_op->opcode == ZEND_FETCH_OBJ_R) {
if (last_op->op2_type == IS_CONST) {
zval name;
name = CONSTANT(last_op->op2.constant);
if (Z_TYPE(name) != IS_STRING) {
zend_error(E_COMPILE_ERROR, "Method name must be a string");
}
if (!IS_INTERNED(Z_STRVAL(name))) {
Z_STRVAL(name) = estrndup(Z_STRVAL(name), Z_STRLEN(name));
}
FREE_POLYMORPHIC_CACHE_SLOT(last_op->op2.constant);
last_op->op2.constant =
zend_add_func_name_literal(CG(active_op_array), &name TSRMLS_CC);
GET_POLYMORPHIC_CACHE_SLOT(last_op->op2.constant);
}
last_op->opcode = ZEND_INIT_METHOD_CALL;
SET_UNUSED(last_op->result);
Z_LVAL(left_bracket->u.constant) = ZEND_INIT_FCALL_BY_NAME;
} else {
}
}

ZEND_INIT_METHOD_CALL 取出函数体EX(fbc) 

static int ZEND_FASTCALL  ZEND_INIT_METHOD_CALL_SPEC_CV_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
{
USE_OPLINE
zval *function_name;
char *function_name_strval;
int function_name_strlen; SAVE_OPLINE();
zend_ptr_stack_3_push(&EG(arg_types_stack), EX(fbc), EX(object), EX(called_scope)); function_name = opline->op2.zv; if (IS_CONST != IS_CONST &&
UNEXPECTED(Z_TYPE_P(function_name) != IS_STRING)) {
zend_error_noreturn(E_ERROR, "Method name must be a string");
} function_name_strval = Z_STRVAL_P(function_name);
function_name_strlen = Z_STRLEN_P(function_name); EX(object) = _get_zval_ptr_cv_BP_VAR_R(EX_CVs(), opline->op1.var TSRMLS_CC); if (EXPECTED(EX(object) != NULL) &&
EXPECTED(Z_TYPE_P(EX(object)) == IS_OBJECT)) {
EX(called_scope) = Z_OBJCE_P(EX(object)); if (IS_CONST != IS_CONST ||
(EX(fbc) = CACHED_POLYMORPHIC_PTR(opline->op2.literal->cache_slot, EX(called_scope))) == NULL) {
zval *object = EX(object); if (UNEXPECTED(Z_OBJ_HT_P(EX(object))->get_method == NULL)) {
zend_error_noreturn(E_ERROR, "Object does not support method calls");
} /* First, locate the function. */ //获取所调用方法的 fbc
EX(fbc) = Z_OBJ_HT_P(EX(object))->get_method(&EX(object), function_name_strval, function_name_strlen, ((IS_CONST == IS_CONST) ? (opline->op2.literal + 1) : NULL) TSRMLS_CC);
if (UNEXPECTED(EX(fbc) == NULL)) {
zend_error_noreturn(E_ERROR, "Call to undefined method %s::%s()", Z_OBJ_CLASS_NAME_P(EX(object)), function_name_strval);
}
if (IS_CONST == IS_CONST &&
EXPECTED(EX(fbc)->type <= ZEND_USER_FUNCTION) &&
EXPECTED((EX(fbc)->common.fn_flags & (ZEND_ACC_CALL_VIA_HANDLER|ZEND_ACC_NEVER_CACHE)) == ) &&
EXPECTED(EX(object) == object)) {
CACHE_POLYMORPHIC_PTR(opline->op2.literal->cache_slot, EX(called_scope), EX(fbc));
}
}
} else {
zend_error_noreturn(E_ERROR, "Call to a member function %s() on a non-object", function_name_strval);
} if ((EX(fbc)->common.fn_flags & ZEND_ACC_STATIC) != ) {
EX(object) = NULL;
} else {
if (!PZVAL_IS_REF(EX(object))) {
Z_ADDREF_P(EX(object)); /* For $this pointer */
} else {
zval *this_ptr;
ALLOC_ZVAL(this_ptr);
INIT_PZVAL_COPY(this_ptr, EX(object));
zval_copy_ctor(this_ptr);
EX(object) = this_ptr;
}
} CHECK_EXCEPTION();
ZEND_VM_NEXT_OPCODE();
}
static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_R(zval ***CVs, zend_uint var TSRMLS_DC)
{
zval ***ptr = &CV(var); if (UNEXPECTED(*ptr == NULL)) {
return *_get_zval_cv_lookup_BP_VAR_R(ptr, var TSRMLS_CC);
}
return **ptr;
} static zend_never_inline zval **_get_zval_cv_lookup_BP_VAR_R(zval ***ptr, zend_uint var TSRMLS_DC)
{
zend_compiled_variable *cv = &CV_DEF_OF(var); if (!EG(active_symbol_table) ||
zend_hash_quick_find(EG(active_symbol_table), cv->name, cv->name_len+, cv->hash_value, (void **)ptr)==FAILURE) {
zend_error(E_NOTICE, "Undefined variable: %s", cv->name);
return &EG(uninitialized_zval_ptr);
}
return *ptr;
}
typedef union _zend_function *(*zend_object_get_method_t)(zval **object_ptr, char *method, int method_len, const struct _zend_literal *key TSRMLS_DC);

static union _zend_function *zend_std_get_method(zval **object_ptr, char *method_name, int method_len, const zend_literal *key TSRMLS_DC) /* {{{ */
{
zend_function *fbc;
zval *object = *object_ptr;
zend_object *zobj = Z_OBJ_P(object);
ulong hash_value;
char *lc_method_name;
ALLOCA_FLAG(use_heap) if (EXPECTED(key != NULL)) {
lc_method_name = Z_STRVAL(key->constant);
hash_value = key->hash_value;
} else {
lc_method_name = do_alloca(method_len+, use_heap);
/* Create a zend_copy_str_tolower(dest, src, src_length); */
zend_str_tolower_copy(lc_method_name, method_name, method_len);
hash_value = zend_hash_func(lc_method_name, method_len+);
} if (UNEXPECTED(zend_hash_quick_find(&zobj->ce->function_table, lc_method_name, method_len+1, hash_value, (void **)&fbc) == FAILURE)) {
if (UNEXPECTED(!key)) {
free_alloca(lc_method_name, use_heap);
}
if (zobj->ce->__call) {
return zend_get_user_call_function(zobj->ce, method_name, method_len);
} else {
return NULL;
}
} /* Check access level */
//如果此方法是私有方法,检查其作用域
if (fbc->op_array.fn_flags & ZEND_ACC_PRIVATE) {
zend_function *updated_fbc; /* Ensure that if we're calling a private function, we're allowed to do so.
* If we're not and __call() handler exists, invoke it, otherwise error out.
*/
updated_fbc = zend_check_private_int(fbc, Z_OBJ_HANDLER_P(object, get_class_entry)(object TSRMLS_CC), lc_method_name, method_len, hash_value TSRMLS_CC);
if (EXPECTED(updated_fbc != NULL)) {
fbc = updated_fbc;
} else {
if (zobj->ce->__call) {
fbc = zend_get_user_call_function(zobj->ce, method_name, method_len);
} else {
zend_error_noreturn(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), method_name, EG(scope) ? EG(scope)->name : "");
}
}
} else {
/* Ensure that we haven't overridden a private function and end up calling
* the overriding public function...
*/     // zend_do_begin_function_declaration
     //   op_array.scope = is_method?CG(active_class_entry):NULL;

       // zend_do_fcall_common_helper_SPEC

        //  EG(scope) = (fbc->type == ZEND_USER_FUNCTION || !EX(object)) ? fbc->common.scope : NULL;

        if (EG(scope) &&
is_derived_class(fbc->common.scope, EG(scope)) &&
fbc->op_array.fn_flags & ZEND_ACC_CHANGED) {
zend_function *priv_fbc; if (zend_hash_quick_find(&EG(scope)->function_table, lc_method_name, method_len+, hash_value, (void **) &priv_fbc)==SUCCESS
&& priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE
&& priv_fbc->common.scope == EG(scope)) {
fbc = priv_fbc;
}
} //是否是protected方法
if ((fbc->common.fn_flags & ZEND_ACC_PROTECTED)) {
/* Ensure that if we're calling a protected function, we're allowed to do so.
* If we're not and __call() handler exists, invoke it, otherwise error out.
*/
if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fbc), EG(scope)))) {
if (zobj->ce->__call) {
fbc = zend_get_user_call_function(zobj->ce, method_name, method_len);
} else {
zend_error_noreturn(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), method_name, EG(scope) ? EG(scope)->name : "");
}
}
}
} if (UNEXPECTED(!key)) {
free_alloca(lc_method_name, use_heap);
}
return fbc;
}

检测调用的方法的作用域,一般来说 父类的private方法 被子类复制,但由于该方法的scope还是父类,所以不能被调用

static inline zend_function *zend_check_private_int(zend_function *fbc, zend_class_entry *ce, char *function_name_strval, int function_name_strlen, ulong hash_value TSRMLS_DC) /* {{{ */
{
if (!ce) {
return ;
} /* We may call a private function if:
* 1. The class of our object is the same as the scope, and the private
* function (EX(fbc)) has the same scope.
* 2. One of our parent classes are the same as the scope, and it contains
* a private function with the same name that has the same scope.
*/
if (fbc->common.scope == ce && EG(scope) == ce) {
/* rule #1 checks out ok, allow the function call */
return fbc;
} /* Check rule #2 */
ce = ce->parent;
while (ce) {
if (ce == EG(scope)) {
if (zend_hash_quick_find(&ce->function_table, function_name_strval, function_name_strlen+, hash_value, (void **) &fbc)==SUCCESS
&& fbc->op_array.fn_flags & ZEND_ACC_PRIVATE
&& fbc->common.scope == EG(scope)) {
return fbc;
}
break;
}
ce = ce->parent;
}
return NULL;
}

 

3.ZEND_DO_END_FUNCTION_CALL 函数

void zend_do_end_function_call(znode *function_name, znode *result, const znode *argument_list, int is_method, int is_dynamic_fcall TSRMLS_DC) /* {{{ */
{
zend_op *opline; if (is_method && function_name && function_name->op_type == IS_UNUSED) {
/* clone */
//省略
} else {
opline = get_next_op(CG(active_op_array) TSRMLS_CC);
if (!is_method && !is_dynamic_fcall && function_name->op_type==IS_CONST) {
//省略
} else {
opline->opcode = ZEND_DO_FCALL_BY_NAME;
SET_UNUSED(opline->op1);
}
} opline->result.var = get_temporary_variable(CG(active_op_array));
opline->result_type = IS_VAR;
GET_NODE(result, opline->result) ;
SET_UNUSED(opline->op2); zend_stack_del_top(&CG(function_call_stack));
opline->extended_value = Z_LVAL(argument_list->u.constant);
}

4. ZEND_DO_FCALL_BY_NAME 函数

static int ZEND_FASTCALL  ZEND_DO_FCALL_BY_NAME_SPEC_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
{
EX(function_state).function = EX(fbc);
return zend_do_fcall_common_helper_SPEC(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);
}

 各种检测,然后将fbc->op_array 赋值给 EG(active_op_array)

static int ZEND_FASTCALL zend_do_fcall_common_helper_SPEC(ZEND_OPCODE_HANDLER_ARGS)
{
USE_OPLINE
zend_bool should_change_scope = ;
zend_function *fbc = EX(function_state).function; SAVE_OPLINE();
if (UNEXPECTED((fbc->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED)) != )) {
if (UNEXPECTED((fbc->common.fn_flags & ZEND_ACC_ABSTRACT) != )) {
zend_error_noreturn(E_ERROR, "Cannot call abstract method %s::%s()", fbc->common.scope->name, fbc->common.function_name);
CHECK_EXCEPTION();
ZEND_VM_NEXT_OPCODE(); /* Never reached */
}
if (UNEXPECTED((fbc->common.fn_flags & ZEND_ACC_DEPRECATED) != )) {
zend_error(E_DEPRECATED, "Function %s%s%s() is deprecated",
fbc->common.scope ? fbc->common.scope->name : "",
fbc->common.scope ? "::" : "",
fbc->common.function_name);
}
}
if (fbc->common.scope &&
!(fbc->common.fn_flags & ZEND_ACC_STATIC) &&
!EX(object)) { if (fbc->common.fn_flags & ZEND_ACC_ALLOW_STATIC) {
/* FIXME: output identifiers properly */
zend_error(E_STRICT, "Non-static method %s::%s() should not be called statically", fbc->common.scope->name, fbc->common.function_name);
} else {
/* FIXME: output identifiers properly */
/* An internal function assumes $this is present and won't check that. So PHP would crash by allowing the call. */
zend_error_noreturn(E_ERROR, "Non-static method %s::%s() cannot be called statically", fbc->common.scope->name, fbc->common.function_name);
}
} if (fbc->type == ZEND_USER_FUNCTION || fbc->common.scope) {
should_change_scope = ;
EX(current_this) = EG(This);
EX(current_scope) = EG(scope);
EX(current_called_scope) = EG(called_scope);
EG(This) = EX(object);
EG(scope) = (fbc->type == ZEND_USER_FUNCTION || !EX(object)) ? fbc->common.scope : NULL;
EG(called_scope) = EX(called_scope);
} zend_arg_types_stack_3_pop(&EG(arg_types_stack), &EX(called_scope), &EX(current_object), &EX(fbc));
EX(function_state).arguments = zend_vm_stack_push_args(opline->extended_value TSRMLS_CC);
LOAD_OPLINE(); if (fbc->type == ZEND_INTERNAL_FUNCTION) {
//内部函数,不用管
} else if (fbc->type == ZEND_USER_FUNCTION) {
EX(original_return_value) = EG(return_value_ptr_ptr);
EG(active_symbol_table) = NULL;
EG(active_op_array) = &fbc->op_array;
EG(return_value_ptr_ptr) = NULL;
if (RETURN_VALUE_USED(opline)) {
temp_variable *ret = &EX_T(opline->result.var); ret->var.ptr = NULL;
EG(return_value_ptr_ptr) = &ret->var.ptr;
ret->var.ptr_ptr = &ret->var.ptr;
ret->var.fcall_returned_reference = (fbc->common.fn_flags & ZEND_ACC_RETURN_REFERENCE) != ;
} if (EXPECTED(zend_execute == execute)) {
if (EXPECTED(EG(exception) == NULL)) {
ZEND_VM_ENTER();
}
}
}
上一篇:hadoop 集群部署ganglia 监控服务与nagios 报警服务


下一篇:Linux环境下将Oracle11g数据库模式由非归档模式(Noarchivelog)修改为自动归档模式(archivelog)