php 对象的执行

1.BNF范式

%token T_OBJECT_OPERATOR "-> (T_OBJECT_OPERATOR)"

unticked_statement:
|    expr ‘;‘                { zend_do_free(&$1 TSRMLS_CC); }

expr:
        r_variable                    { $$ = $1; }
    |    expr_without_variable        { $$ = $1; }
;

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

object_property:
        object_dim_list { $$ = $1; }
    |    variable_without_objects { zend_do_end_variable_parse(&$1, BP_VAR_R, 0 TSRMLS_CC); } { znode tmp_znode;  zend_do_pop_object(&tmp_znode TSRMLS_CC);  zend_do_fetch_property(&$$, &tmp_znode, &$1 TSRMLS_CC);}
;

variable_without_objects:
        reference_variable { $$ = $1; }
    |    simple_indirect_reference reference_variable { zend_do_indirect_references(&$$, &$1, &$2 TSRMLS_CC); }
;

reference_variable:
        reference_variable ‘[‘ dim_offset ‘]‘    { fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
    |    reference_variable ‘{‘ expr ‘}‘        { fetch_string_offset(&$$, &$1, &$3 TSRMLS_CC); }
    |    compound_variable            { zend_do_begin_variable_parse(TSRMLS_C); fetch_simple_variable(&$$, &$1, 1 TSRMLS_CC); }
;

compound_variable:
        T_VARIABLE            { $$ = $1; }
    |    ‘$‘ ‘{‘ expr ‘}‘    { $$ = $3; }
;

 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                        { $$ = $1; $$.EA = ZEND_PARSED_METHOD_CALL; zend_do_push_object(&$$ TSRMLS_CC); }
    |    array_method_dereference    { $$ = $1; zend_do_push_object(&$$ TSRMLS_CC); }
    |    /* empty */ { $$.EA = ZEND_PARSED_MEMBER; }
;

method:
        ‘(‘ { zend_do_pop_object(&$1 TSRMLS_CC); zend_do_begin_method_call(&$1 TSRMLS_CC); }
                function_call_parameter_list ‘)‘
            { zend_do_end_function_call(&$1, &$$, &$3, 1, 1 TSRMLS_CC); zend_do_extended_fcall_end(TSRMLS_C); }
;

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))-1; //$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)) == 0) &&
                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) != 0) {
        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+1, 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+1, 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+1);
    }

    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+1, 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 0;
    }

    /* 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+1, 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 = 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) {
    //内部函数,不用管
    } 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();
            }
        }
}
时间: 2024-10-05 08:38:34

php 对象的执行的相关文章

JQuery选中的对象和非选中的其他对象分别执行不同动作

$("#searchWeek,#searchFifteen,#searchThirty").click(function(){ $("#searchBox").val($(this).html()); $(this).addClass('select').siblings('span').removeClass('select'); }); 选中的元素添加select class  其余的移除select class. End. JQuery选中的对象和非选中的其他

javascript数据类型(六)--- 函数对象之执行上下文

一.引入 1.1 变量声明提升 通过var定义(声明)的变量, 在定义语句之前就可以访问到,其值为: undefined var a = 3 function fn () { console.log(a) //输出结果为undefined var a = 4 } fn() 1.2 函数声明提升 通过function声明的函数, 在之前就可以直接调用,其值为: 函数定义(对象) console.log(b) //undefined 变量提升 fn2() //可调用 函数提升 // fn3() //

API解读第二篇——执行SQL的核心对象(JDBC)

结构图 核心对象 Statement Statement主要用来执行SQL语句.它执行SQL语句的步骤为: 第一步:创建statement对象. 第二步:配置statement对象,此步骤可以忽略. 第三步:调用statement的executeXXX方法执行SQL语句. 第四步:处理结果集.示例中只是打印结果集. 第五步:关闭statement对象. Statement执行SQL语句的方法有四种类型. execute执行返回结果包含一个或者多个结果集,或结果包含一个或多个更新记录数量的SQL语

*JS:执行环境、变量对象、活动对象和作用域链

var a=1; function b(x){ var c=2; console.log(x); } b(3); ·执行环境(execution context),也称为环境.执行上下文.上下文环境.执行上下文环境: 每次当控制器转到ECMAScript可执行代码的时候,即会进入到一个执行上下文.执行上下文(简称-EC)是ECMA-262标准里的一个抽象概念,用于同可执行代码(executable code)概念进行区分. 通俗的话来讲就是,JS中的函数运行不能仅仅看函数内部有哪些变量,再简单的

Js使用WScript.Shell对象执行.bat文件和cmd命令

http://www.jb51.net/article/58669.htm WScript.Shell(Windows Script Host Runtime Library)是一个对象,对应的文件是C:/WINDOWS/system32/wshom.ocx,Wscript.shell是服务器系统会用到的一种组件.shell 就是“壳”的意思,这个对象可以执行操作系统外壳常用的操作,比如运行程序.读写注册表.环境变量等.这个对象通常被用在VB或VBS编程中. 安装WScript.Shell对象:

Javascript中使用WScript.Shell对象执行.bat文件和cmd命令

Javascript中使用WScript.Shell对象执行.bat文件和cmd命令 http://www.cnblogs.com/ZHF/p/3328439.html WScript.Shell(Windows Script Host Runtime Library)是一个对象,对应的文件是C:/WINDOWS/system32/wshom.ocx,Wscript.shell是服务器系统会用到的一种组件.shell 就是“壳”的意思,这个对象可以执行操作系统外壳常用的操作,比如运行程序.读写注

PreparedStatement预编译的sql执行对象

一.预编译,防sql注入 其中,设置参数值占位符索引从1开始:在由sql 连接对象创建 sql执行对象时候传入参数sql语句,在执行对象在执行方法时候就不用再传入sql语句: 数据库索引一般是从1开始,java对象一般是从0开始: java代码方法subString(2,5)是左闭右开区间,数据库subString(str, 5) 是从5开始截取到最后: 原文地址:https://www.cnblogs.com/wmqiang/p/11600768.html

关于多个Promise对象及then()函数的执行顺序的研究记录

今天终于想要研究一下多个 Promise 对象的执行顺序问题了,在研究完后记录一下. 我想研究的是以下问题: 1.多个 Promise 对象及其then函数的执行顺序,这里不研究处于不同状态的 Promise 对象的执行顺序 2.在 Promise 中的定时器延时问题(这个问题其实在 MDN 和阮一峰老师的 ES6 入门中都讲过,只是我光看文字有点晕,所以自己写代码来理解) 废话不多说,先上代码吧 // 延时执行 new Promise(resolve => setTimeout(() =>

ElasticSearch入门 第六篇:复合数据类型——数组,对象和嵌套

这是ElasticSearch 2.4 版本系列的第六篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 ElasticSearch入门 第三篇:索引 ElasticSearch入门 第四篇:使用C#添加和更新文档 ElasticSearch入门 第五篇:使用C#查询文档 ElasticSearch入门 第六篇:复合数据类型——数组,对象和嵌套 在ElasticSearch中,使用JSON结构来存储数据,