在面向对象语言中,都会内置一些语言内置提供的基本功能类,比如JavaScript中的Array,Number等类, PHP中也有很多这种类,比如Directory,stdClass,Exception等类,同时一些标准扩展比如PDO等扩展中也会定义一些类, PHP中类是不允许重复定义的,所以在编写代码时不允许定义已经存在的类。
同时PHP中有一些特殊的类:self,static和parent,相信读者对这self和parent都比较熟悉了,而static特殊类是PHP5.3才引入的。
PHP中的static关键字非常多义:
- 在函数体内的修饰变量的static关键字用于定义静态局部变量。
- 用于修饰类成员函数和成员变量时用于声明静态成员。
- (PHP5.3)在作用域解析符(::)前又表示静态延迟绑定的特殊类。
这个关键字修饰的意义都表示"静态",在PHP手册中提到self, parent和static这几个关键字,但实际上除了static是关键字以外,其他两个均不是关键字, 在手册的关键字列表中也没有这两个关键字, 要验证这一点很简单:
上面的代码并没有报错,如果你把error_reporting(E_ALL)打开,就能看到实际是什么情况了: 运行这段代码会出现“ Notice: Use of undefined constant self - assumed 'self'“, 也就是说PHP把self当成一个普通常量了,尝试未定义的常量会把产量本身当成一个字符串, 例如上例的”self",不过同时会出一个NOTICE,这就是说self这个标示符并没有什么特殊的。
2 | define( 'self' , "stdClass" ); |
不同语言中的关键字的意义会有些区别,Wikipedia上的解释是: 具有特殊含义的标示符或者单词,从这个意义上说$this也算是一个关键字,但在PHP的关键字列表中并没有。 PHP的关键字和C/C++一样属于保留字(关键字),关键字用于表示特定的语法形式,例如函数定义,流程控制等结构。 这些关键字有他们的特定的使用场景,而上面提到的self和parent并没有这样的限制。
self,parent,static类
前面已经说过self的特殊性。self是一个特殊类,它指向当前类,但只有在类定义内部才有效, 但也并不一定指向类本身这个特殊类,比如前面的代码,如果放在类方法体内运行,echo self; 还是会输出常量self的值,而不是当前类,它不止要求在类的定义内部,还要求在类的上下文环境, 比如 new self()的时候,这时self就指向当前类,或者self::$static_varible, self::CONSTANT类似的作用域解析符号(::),这时的self才会作为指向本身的类而存在。
同理parent也和self类似。下面先看看在在类的环境下的编译吧$PHP_SRC/Zend/zend_language_parser.y:
2 | class_name { zend_do_fetch_class(&$$, &$1 TSRMLS_CC); } |
3 | | dynamic_class_name_reference { zend_do_end_variable_parse(&$1, BP_VAR_R, 0 TSRMLS_CC); zend_do_fetch_class(&$$, &$1 TSRMLS_CC); } |
在需要获取类名时会执行zend_do_fetch_class()函数:
01 | void zend_do_fetch_class(znode *result, znode *class_name TSRMLS_DC) |
04 | opline->opcode = ZEND_FETCH_CLASS; |
05 | if (class_name->op_type == IS_CONST) { |
08 | fetch_type = zend_get_class_fetch_type(class_name->u.constant.value.str.val, class_name->u.constant.value.str.len); |
10 | case ZEND_FETCH_CLASS_SELF: |
11 | case ZEND_FETCH_CLASS_PARENT: |
12 | case ZEND_FETCH_CLASS_STATIC: |
13 | SET_UNUSED(opline->op2); |
14 | opline->extended_value = fetch_type; |
15 | zval_dtor(&class_name->u.constant); |
18 | zend_resolve_class_name(class_name, &opline->extended_value, 0 TSRMLS_CC); |
19 | opline->op2 = *class_name; |
23 | opline->op2 = *class_name; |
上面省略了一些无关的代码,重点关注fetch_type变量。这是通过zend_get_class_fetch_type()函数获取到的。
01 | int zend_get_class_fetch_type( const char *class_name, uint class_name_len) |
03 | if ((class_name_len == sizeof ( "self" )-1) && |
04 | ! memcmp (class_name, "self" , sizeof ( "self" )-1)) { |
05 | return ZEND_FETCH_CLASS_SELF; |
06 | } else if ((class_name_len == sizeof ( "parent" )-1) && |
07 | ! memcmp (class_name, "parent" , sizeof ( "parent" )-1)) { |
08 | return ZEND_FETCH_CLASS_PARENT; |
09 | } else if ((class_name_len == sizeof ( "static" )-1) && |
10 | ! memcmp (class_name, "static" , sizeof ( "static" )-1)) { |
11 | return ZEND_FETCH_CLASS_STATIC; |
13 | return ZEND_FETCH_CLASS_DEFAULT; |
前面的代码是Zend引擎编译类相关操作的代码,下面就到执行阶段了,self,parent等类的指向会在执行时进行获取, 找到执行opcode为ZEND_FETCH_CLASS的执行函数:
01 | zend_class_entry *zend_fetch_class( const char *class_name, uint class_name_len, int fetch_type TSRMLS_DC) |
03 | zend_class_entry **pce; |
04 | int use_autoload = (fetch_type & ZEND_FETCH_CLASS_NO_AUTOLOAD) == 0; |
05 | int silent = (fetch_type & ZEND_FETCH_CLASS_SILENT) != 0; |
07 | fetch_type &= ZEND_FETCH_CLASS_MASK; |
11 | case ZEND_FETCH_CLASS_SELF: |
13 | zend_error(E_ERROR, "Cannot access self:: when no class scope is active" ); |
16 | case ZEND_FETCH_CLASS_PARENT: |
18 | zend_error(E_ERROR, "Cannot access parent:: when no class scope is active" ); |
20 | if (!EG(scope)->parent) { |
21 | zend_error(E_ERROR, "Cannot access parent:: when current class scope has no parent" ); |
23 | return EG(scope)->parent; |
24 | case ZEND_FETCH_CLASS_STATIC: |
25 | if (!EG(called_scope)) { |
26 | zend_error(E_ERROR, "Cannot access static:: when no class scope is active" ); |
28 | return EG(called_scope); |
29 | case ZEND_FETCH_CLASS_AUTO: { |
30 | fetch_type = zend_get_class_fetch_type(class_name, class_name_len); |
31 | if (fetch_type!=ZEND_FETCH_CLASS_DEFAULT) { |
32 | goto check_fetch_type; |
38 | if (zend_lookup_class_ex(class_name, class_name_len, use_autoload, &pce TSRMLS_CC) == FAILURE) { |
40 | if (!silent && !EG(exception)) { |
41 | if (fetch_type == ZEND_FETCH_CLASS_INTERFACE) { |
42 | zend_error(E_ERROR, "Interface '%s' not found" , class_name); |
44 | zend_error(E_ERROR, "Class '%s' not found" , class_name); |
从这个函数就能看出端倪了,当需要获取self类的时候,则将EG(scope)类返回,而EG(scope)指向的正是当前类。 如果时parent类的话则从去EG(scope)->parent也就是当前类的父类,而static获取的时EG(called_scope), 分别说说EG宏的这几个字段,前面已经介绍过EG宏,它可以展开为如下这个结构体:
01 | struct _zend_executor_globals { |
03 | zend_class_entry *scope; |
04 | zend_class_entry *called_scope; |
08 | struct _zend_class_entry { |
11 | zend_uint name_length; |
12 | struct _zend_class_entry *parent; |
14 | #define struct _zend_class_entry zend_class_entry |
其中的zend_class_entry就是PHP中类的内部结构表示,zend_class_entry有一个parent字段,也就是该类的父类。 在EG结构体中的中called_scope会在执行过程中将当前执行的类赋值给called_scope,例如如下代码:
03 | public static funcA() { |
09 | public static funcB() { |
代码B::funcA()执行的时候,实际执行的是B的父类A中定义的funcA函数,A::funcA()执行时当前的类(scope)指向的是类A, 而这个方法是从B类开始调用的,called_scope指向的是类B,static特殊类指向的正是called_scope,也就是当前类(触发方法调用的类), 这也是延迟绑定的原理。
没有帐号? 现在注册.