php 对象 调用静态方法

 

1.BNF范式

%token T_PAAMAYIM_NEKUDOTAYIM ":: (T_PAAMAYIM_NEKUDOTAYIM)"

//类名::静态方法(...);
function_call:
| class_name T_PAAMAYIM_NEKUDOTAYIM variable_name '(' { $4.u.op.opline_num = zend_do_begin_class_member_function_call(&$1, &$3 TSRMLS_CC); } function_call_parameter_list ')' { zend_do_end_function_call($4.u.op.opline_num?NULL:&$3, &$$, &$6, $4.u.op.opline_num, $4.u.op.opline_num TSRMLS_CC); zend_do_extended_fcall_end(TSRMLS_C);}

 

2.调用静态方法的编译 zend_do_begin_class_member_function_call

int zend_do_begin_class_member_function_call(znode *class_name, znode *method_name TSRMLS_DC) /* {{{ */
{
    znode class_node;
    unsigned char *ptr = NULL;
    zend_op *opline;

    if (method_name->op_type == IS_CONST) {
        char *lcname;
        if (Z_TYPE(method_name->u.constant) != IS_STRING) {
            zend_error(E_COMPILE_ERROR, "Method name must be a string");
        }
        lcname = zend_str_tolower_dup(Z_STRVAL(method_name->u.constant), Z_STRLEN(method_name->u.constant));
        if ((sizeof(ZEND_CONSTRUCTOR_FUNC_NAME)-1) == Z_STRLEN(method_name->u.constant) &&
            memcmp(lcname, ZEND_CONSTRUCTOR_FUNC_NAME, sizeof(ZEND_CONSTRUCTOR_FUNC_NAME)-1) == 0) {
            zval_dtor(&method_name->u.constant);
            method_name->op_type = IS_UNUSED;
        }
        efree(lcname);
    }

    if (class_name->op_type == IS_CONST &&
        ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type(Z_STRVAL(class_name->u.constant), Z_STRLEN(class_name->u.constant))) {
        zend_resolve_class_name(class_name, ZEND_FETCH_CLASS_GLOBAL, 1 TSRMLS_CC);
        class_node = *class_name;
        opline = get_next_op(CG(active_op_array) TSRMLS_CC);
    } else {
        zend_do_fetch_class(&class_node, class_name TSRMLS_CC);
        opline = get_next_op(CG(active_op_array) TSRMLS_CC);
        opline->extended_value = class_node.EA    ;
    }
    opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
    if (class_node.op_type == IS_CONST) {
        opline->op1_type = IS_CONST;
        opline->op1.constant =
            zend_add_class_name_literal(CG(active_op_array), &class_node.u.constant TSRMLS_CC); //op1.constant为数字,以方便其handler使用
    } else {
        SET_NODE(opline->op1, &class_node);
    }
    if (method_name->op_type == IS_CONST) {
        opline->op2_type = IS_CONST;
        opline->op2.constant =
            zend_add_func_name_literal(CG(active_op_array), &method_name->u.constant TSRMLS_CC); //op2.constant为数字,以方便其handler使用
        if (opline->op1_type == IS_CONST) {
            GET_CACHE_SLOT(opline->op2.constant);
        } else {
            GET_POLYMORPHIC_CACHE_SLOT(opline->op2.constant);
        }
    } else {
        SET_NODE(opline->op2, method_name);
    }

    //代码忽略
    return 1; /* Dynamic */
}

static int ZEND_FASTCALL  ZEND_INIT_STATIC_METHOD_CALL_SPEC_CONST_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
{
    USE_OPLINE
    zval *function_name;
    zend_class_entry *ce;

    SAVE_OPLINE();
    zend_ptr_stack_3_push(&EG(arg_types_stack), EX(fbc), EX(object), EX(called_scope));

    if (IS_CONST == IS_CONST) {
        /* no function found. try a static method in class */
        if (CACHED_PTR(opline->op1.literal->cache_slot)) { //如果EG(active_op_array)->run_time_cache[]数组中存在这个值,就取出来,毕竟C原生态数组取数据速度要远远超过zend_hash_quick_find(毕竟他要计算hash值,还要遍历,不能达到真正的O(1))
            ce = CACHED_PTR(opline->op1.literal->cache_slot);
        } else {
            ce = zend_fetch_class_by_name(Z_STRVAL_P(opline->op1.zv), Z_STRLEN_P(opline->op1.zv), opline->op1.literal + 1, opline->extended_value TSRMLS_CC);
            if (UNEXPECTED(ce == NULL)) {
                CHECK_EXCEPTION();
                ZEND_VM_NEXT_OPCODE();
            }
            CACHE_PTR(opline->op1.literal->cache_slot, ce); //放入EG(active_op_array)->run_time_cache[]这个数组中,以便下次调用时提高速度
        }
        EX(called_scope) = ce;
    } else {
        //代码忽略
    }

    if (IS_CONST == IS_CONST &&
        IS_CONST == IS_CONST &&
        CACHED_PTR(opline->op2.literal->cache_slot)) {
        EX(fbc) = CACHED_PTR(opline->op2.literal->cache_slot);
    } else if (IS_CONST != IS_CONST &&
               IS_CONST == IS_CONST &&
               (EX(fbc) = CACHED_POLYMORPHIC_PTR(opline->op2.literal->cache_slot, ce))) {
        /* do nothing */
    } else if (IS_CONST != IS_UNUSED) {
        char *function_name_strval = NULL;
        int function_name_strlen = 0;


        if (IS_CONST == IS_CONST) {
            function_name_strval = Z_STRVAL_P(opline->op2.zv);
            function_name_strlen = Z_STRLEN_P(opline->op2.zv);
        } else {
            //代码忽略
        }

        if (function_name_strval) {
            if (ce->get_static_method) {
                EX(fbc) = ce->get_static_method(ce, function_name_strval, function_name_strlen TSRMLS_CC);
            } else {
                EX(fbc) = zend_std_get_static_method(ce, 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()", ce->name, 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)) == 0)) { if (IS_CONST == IS_CONST) { CACHE_PTR(opline->op2.literal->cache_slot, EX(fbc));//放到EG(active_op_array)->run_time_cache[]数组中 } else { CACHE_POLYMORPHIC_PTR(opline->op2.literal->cache_slot, ce, EX(fbc)); } } } if (IS_CONST != IS_CONST) { } } else { //代码省略 } //代码省略 CHECK_EXCEPTION(); ZEND_VM_NEXT_OPCODE(); }

 

 

zend_class_entry *zend_fetch_class_by_name(const char *class_name, uint class_name_len, const zend_literal *key, int fetch_type TSRMLS_DC) /* {{{ */
{
    zend_class_entry **pce;
    int use_autoload = (fetch_type & ZEND_FETCH_CLASS_NO_AUTOLOAD) == 0;

    if (zend_lookup_class_ex(class_name, class_name_len, key, use_autoload, &pce TSRMLS_CC) == FAILURE) {
        
    }
    return *pce;
}


ZEND_API int zend_lookup_class_ex(const char *name, int name_length, const zend_literal *key, int use_autoload, zend_class_entry ***ce TSRMLS_DC) /* {{{ */
{
    zval **args[1];
    zval autoload_function;
    zval *class_name_ptr;
    zval *retval_ptr = NULL;
    int retval, lc_length;
    char *lc_name;
    char *lc_free;
    zend_fcall_info fcall_info;
    zend_fcall_info_cache fcall_cache;
    char dummy = 1;
    ulong hash;
    ALLOCA_FLAG(use_heap)

    if (key) {
        lc_name = Z_STRVAL(key->constant);
        lc_length = Z_STRLEN(key->constant) + 1;
        hash = key->hash_value;
    } else {
        
        lc_free = lc_name = do_alloca(name_length + 1, use_heap);
        zend_str_tolower_copy(lc_name, name, name_length);
        lc_length = name_length + 1;

        if (lc_name[0] == '\\') {
            lc_name += 1;
            lc_length -= 1;
        }

        hash = zend_inline_hash_func(lc_name, lc_length);
    }

    if (zend_hash_quick_find(EG(class_table), lc_name, lc_length, hash, (void **) ce) == SUCCESS) { //从EG(class_table)中找到ce
        if (!key) {
            free_alloca(lc_free, use_heap);
        }
        return SUCCESS;
    }
}

 

 

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) {
            opline->opcode = ZEND_DO_FCALL;
            SET_NODE(opline->op1, function_name);
            CALCULATE_LITERAL_HASH(opline->op1.constant);
            GET_CACHE_SLOT(opline->op1.constant);
        } 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);
}

 

 

 

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);
}


static int ZEND_FASTCALL zend_do_fcall_common_helper_SPEC(ZEND_OPCODE_HANDLER_ARGS)
{
    USE_OPLINE
    zend_bool should_change_scope = 0;
    zend_function *fbc = EX(function_state).function;

    SAVE_OPLINE();
    if (UNEXPECTED((fbc->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED)) != 0)) {
        if (UNEXPECTED((fbc->common.fn_flags & ZEND_ACC_ABSTRACT) != 0)) {
            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) != 0)) {
            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 = 1;
        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) {

        //php内部函数

    } 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) != 0;
        }

        if (EXPECTED(zend_execute == execute)) {
            if (EXPECTED(EG(exception) == NULL)) {
                ZEND_VM_ENTER();
            }
        } 

    } else { /* ZEND_OVERLOADED_FUNCTION */
        //代码省略
    }
}

 

posted @ 2019-03-22 11:12  taek  阅读(1784)  评论(0编辑  收藏  举报