稍后在同一文件中定义的派生类"不存在"?

Mic*_*ati 18 php extends class require include

假设我们有两个php文件,a.php和b.php这里是文件a.php的内容:

<?php // content of a.php
class A {
}
Run Code Online (Sandbox Code Playgroud)

这是文件b.php的内容

<?php  // content of b.php
include dirname(__FILE__) . "/a.php";
echo "A: ", class_exists("A") ? "exists" : "doesn’t exist", "\n";
echo "B: ", class_exists("B") ? "exists" : "doesn’t exist", "\n";
echo "BA (before): ", class_exists("BA") ? "exists" : "doesn’t exist", "\n";
echo "BB: ", class_exists("BB") ? "exists" : "doesn’t exist", "\n";
class B {
}
class BA extends A {
}
class BB extends B {
}
echo "BA (after): ", class_exists("BA") ? "exists" : "doesn’t exist", "\n";
Run Code Online (Sandbox Code Playgroud)

如果启动b.php脚本,则输出为:

A: exists
B: exists
BA (before): doesn’t exist
BB: exists
BA (after): exists
Run Code Online (Sandbox Code Playgroud)

为什么BA类只在类定义之后才存在?为什么其他类甚至在定义之前就已存在?有什么区别?我希望在这两种情况下都有一个共同的行为......有没有办法在定义之前就可以使用BA类?

谢谢

米歇尔

Jon*_*Jon 7

免责声明:我并不声称理解Zend的内部运作.以下是我对PHP源代码的解释,主要是通过有根据的猜测推动的.即使我对结论充满信心,但术语或细节可能会被取消.我很想听听任何有关Zend内部经验的人的意见.

调查

从PHP解析器中我们可以看到,当遇到类声明时,zend_do_early_binding会调用该函数.以下是处理派生类声明的代码:

case ZEND_DECLARE_INHERITED_CLASS:
{
    zend_op *fetch_class_opline = opline-1;
    zval *parent_name;
    zend_class_entry **pce;

    parent_name = &CONSTANT(fetch_class_opline->op2.constant);
    if ((zend_lookup_class(Z_STRVAL_P(parent_name), Z_STRLEN_P(parent_name), &pce TSRMLS_CC) == FAILURE) ||
        ((CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES) &&
         ((*pce)->type == ZEND_INTERNAL_CLASS))) {
        if (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING) {
            zend_uint *opline_num = &CG(active_op_array)->early_binding;

            while (*opline_num != -1) {
                opline_num = &CG(active_op_array)->opcodes[*opline_num].result.opline_num;
            }
            *opline_num = opline - CG(active_op_array)->opcodes;
            opline->opcode = ZEND_DECLARE_INHERITED_CLASS_DELAYED;
            opline->result_type = IS_UNUSED;
            opline->result.opline_num = -1;
        }
        return;
    }
    if (do_bind_inherited_class(CG(active_op_array), opline, CG(class_table), *pce, 1 TSRMLS_CC) == NULL) {
        return;
    }
    /* clear unnecessary ZEND_FETCH_CLASS opcode */
    zend_del_literal(CG(active_op_array), fetch_class_opline->op2.constant);
    MAKE_NOP(fetch_class_opline);

    table = CG(class_table);
    break;
}
Run Code Online (Sandbox Code Playgroud)

此代码立即调用zend_lookup_class以查看父类是否存在于符号表中...然后根据是否找到父类而发散.

让我们先来看看如果父类它发现:

if (do_bind_inherited_class(CG(active_op_array), opline, CG(class_table), *pce, 1 TSRMLS_CC) == NULL) {
    return;
}
Run Code Online (Sandbox Code Playgroud)

转到do_bind_inherited_class,我们看到最后一个参数(在这个调用中是1)被调用compile_time.这听起来很有趣.这个论点怎么办?

if (compile_time) {
    op1 = &CONSTANT_EX(op_array, opline->op1.constant);
    op2 = &CONSTANT_EX(op_array, opline->op2.constant);
} else {
    op1 = opline->op1.zv;
    op2 = opline->op2.zv;
}

found_ce = zend_hash_quick_find(class_table, Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_HASH_P(op1), (void **) &pce);

if (found_ce == FAILURE) {
    if (!compile_time) {
        /* If we're in compile time, in practice, it's quite possible
         * that we'll never reach this class declaration at runtime,
         * so we shut up about it.  This allows the if (!defined('FOO')) { return; }
         * approach to work.
         */
        zend_error(E_COMPILE_ERROR, "Cannot redeclare class %s", Z_STRVAL_P(op2));
    }
    return NULL;
} else {
    ce = *pce;
}
Run Code Online (Sandbox Code Playgroud)

好的......所以它从静态(从PHP用户的角度来看)或动态上下文中读取父类和派生类名,具体取决于compile_time状态.然后,它试图找到在类表中的类条目("CE"),并且如果它没有找到,那么...... 它返回而不做在编译的时候什么,但在运行时发出致命错误.

这听起来非常重要.我们回去吧zend_do_early_binding.如果找不到父类,它会怎么做?

if (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING) {
    zend_uint *opline_num = &CG(active_op_array)->early_binding;

    while (*opline_num != -1) {
        opline_num = &CG(active_op_array)->opcodes[*opline_num].result.opline_num;
    }
    *opline_num = opline - CG(active_op_array)->opcodes;
    opline->opcode = ZEND_DECLARE_INHERITED_CLASS_DELAYED;
    opline->result_type = IS_UNUSED;
    opline->result.opline_num = -1;
}
return;
Run Code Online (Sandbox Code Playgroud)

它似乎正在生成操作码即会触发呼叫do_bind_inherited_class再次-但这个时候,价值compile_time将是0(假).

最后,class_existsPHP函数的实现怎么样?查看源代码会显示以下代码段:

found = zend_hash_find(EG(class_table), name, len+1, (void **) &ce);
Run Code Online (Sandbox Code Playgroud)

大!这个class_table变量与我们之前看到class_tabledo_bind_inherited_class调用相同!所以的返回值class_exists取决于是否为类的条目已经被插入class_tabledo_bind_inherited_class.

结论

Zend编译器include在编译时不对指令起作用(即使文件名是硬编码的).

如果确实如此,则根据compile_time未设置的标志,没有理由发出类重新声明致命错误; 错误可以无条件地发出.

当编译器遇到派生类声明,其中基类尚未在同一脚本文件中声明时,它会将在其内部数据结构中注册类的行为推送到运行时.

从上面的最后一个代码片段可以看出这一点,该代码片段设置了一个ZEND_DECLARE_INHERITED_CLASS_DELAYED操作码来在脚本执行时注册该类.此时compile_time标志将是false,行为将略有不同.

返回值class_exists取决于该类是否已经注册.

由于这在编译时和运行时以不同的方式发生,因此行为class_exists也不同:

  • 祖先都包含在同一源文件中的类在编译时注册; 它们存在并且可以在该脚本中的任何位置实例化
  • 具有在另一个源文件中定义的祖先的类在运行时注册; 在VM执行与源中的类定义相对应的操作码之前,这些类不存在用于所有实际目的(class_exists返回false,实例化会产生致命错误)