<?php
class test
{
static $name="abc";
} echo test::$name;

1.巴斯科范式

class_statement:
variable_modifiers { CG(access_type) = Z_LVAL($.u.constant); } class_variable_declaration ';'
; member_modifier:
T_PUBLIC { Z_LVAL($$.u.constant) = ZEND_ACC_PUBLIC; }
| T_PROTECTED { Z_LVAL($$.u.constant) = ZEND_ACC_PROTECTED; }
| T_PRIVATE { Z_LVAL($$.u.constant) = ZEND_ACC_PRIVATE; }
| T_STATIC { Z_LVAL($$.u.constant) = ZEND_ACC_STATIC; }
| T_ABSTRACT { Z_LVAL($$.u.constant) = ZEND_ACC_ABSTRACT; }
| T_FINAL { Z_LVAL($$.u.constant) = ZEND_ACC_FINAL; }
; class_variable_declaration:
class_variable_declaration ',' T_VARIABLE { zend_do_declare_property(&$, NULL, CG(access_type) TSRMLS_CC); }
| class_variable_declaration ',' T_VARIABLE '=' static_scalar { zend_do_declare_property(&$, &$, CG(access_type) TSRMLS_CC); }
| T_VARIABLE { zend_do_declare_property(&$, NULL, CG(access_type) TSRMLS_CC); }
| T_VARIABLE '=' static_scalar { zend_do_declare_property(&$, &$, CG(access_type) TSRMLS_CC); }
; variable:
T_STRING T_PAAMAYIM_NEKUDOTAYIM base_variable { $$ = $; zend_do_fetch_static_member(&$$, &$ TSRMLS_CC); }
; base_variable:
T_VARIABLE { fetch_simple_variable(&$$, &$, TSRMLS_CC); }
;

理解版

class_statement:
variable_modifiers { CG(access_type) = Z_LVAL($.u.constant); } class_variable_declaration ';'
; member_modifier:
T_PUBLIC { Z_LVAL($$.u.constant) = ZEND_ACC_PUBLIC; }
| T_PROTECTED { Z_LVAL($$.u.constant) = ZEND_ACC_PROTECTED; }
| T_PRIVATE { Z_LVAL($$.u.constant) = ZEND_ACC_PRIVATE; }
| T_STATIC { Z_LVAL($$.u.constant) = ZEND_ACC_STATIC; }
| T_ABSTRACT { Z_LVAL($$.u.constant) = ZEND_ACC_ABSTRACT; }
| T_FINAL { Z_LVAL($$.u.constant) = ZEND_ACC_FINAL; }
; class_variable_declaration:
class_variable_declaration ',' T_VARIABLE { zend_do_declare_property(&$, NULL, CG(access_type) TSRMLS_CC); }
| class_variable_declaration ',' T_VARIABLE '=' static_scalar { zend_do_declare_property(&$, &$, CG(access_type) TSRMLS_CC); }
| T_VARIABLE { zend_do_declare_property(&$, NULL, CG(access_type) TSRMLS_CC); }
| T_VARIABLE '=' static_scalar { zend_do_declare_property(&$, &$, CG(access_type) TSRMLS_CC); }
; variable:
T_STRING T_PAAMAYIM_NEKUDOTAYIM T_VARIABLE { $$ = $; zend_do_fetch_static_member(&$$, &$ TSRMLS_CC); }
;

2.对于 static $name="abc"

组织propert_info结构体,将其存于为类中的property_info 这个HashTable中,

注意: value 这个值是存在类中的property_table中,其中key 为property_info中的offset

void zend_do_declare_property(const znode *var_name, const znode *value, zend_uint access_type TSRMLS_DC) /* {{{ */
{
zval *property;
ALLOC_ZVAL(property);
if (value) {
*property = value->u.constant;
} else {
INIT_PZVAL(property);
Z_TYPE_P(property) = IS_NULL;
}
zend_declare_property_ex(CG(active_class_entry), zend_new_interned_string(Z_STRVAL(var_name->u.constant), Z_STRLEN(var_name->u.constant) + , TSRMLS_CC), Z_STRLEN(var_name->u.constant), property, access_type, comment, comment_len TSRMLS_CC);
}
ZEND_API int zend_declare_property_ex(zend_class_entry *ce, const char *name, int name_length, zval *property, int access_type, const char *doc_comment, int doc_comment_len TSRMLS_DC) /* {{{ */
{
zend_property_info property_info, *property_info_ptr;
const char *interned_name;
ulong h = zend_get_hash_value(name, name_length+); if (access_type & ZEND_ACC_STATIC) {
//如果是静态成员变量,就使用relloc分配需要的内存,size(zval*)*ce->default_static_member_count是总共的内存
property_info.offset = ce->default_static_members_count++;
ce->default_static_members_table = perealloc(ce->default_static_members_table, sizeof(zval*) * ce->default_static_members_count, ce->type == ZEND_INTERNAL_CLASS); ce->default_static_members_table[property_info.offset] = property;
if (ce->type == ZEND_USER_CLASS) {
ce->static_members_table = ce->default_static_members_table;
}
} else {
property_info.offset = ce->default_properties_count++;
ce->default_properties_table = perealloc(ce->default_properties_table, sizeof(zval*) * ce->default_properties_count, ce->type == ZEND_INTERNAL_CLASS);
ce->default_properties_table[property_info.offset] = property;
} interned_name = zend_new_interned_string(property_info.name, property_info.name_length+, TSRMLS_CC);
if (interned_name != property_info.name) {
if (ce->type == ZEND_USER_CLASS) {
efree((char*)property_info.name);
} else {
free((char*)property_info.name);
}
property_info.name = interned_name;
} property_info.flags = access_type;
property_info.h = (access_type & ZEND_ACC_PUBLIC) ? h : zend_get_hash_value(property_info.name, property_info.name_length+); property_info.ce = ce;
zend_hash_quick_update(&ce->properties_info, name, name_length+, h, &property_info, sizeof(zend_property_info), NULL);
return SUCCESS;
}
void zend_do_fetch_static_member(znode *result, znode *class_name TSRMLS_DC) /* {{{ */
{
znode class_node;
zend_llist *fetch_list_ptr;
zend_llist_element *le;
zend_op *opline_ptr;
zend_op opline; zend_do_fetch_class(&class_node, class_name TSRMLS_CC); if (result->op_type == IS_CV) {
init_op(&opline TSRMLS_CC); opline.opcode = ZEND_FETCH_W;
opline.result_type = IS_VAR;
opline.result.var = get_temporary_variable(CG(active_op_array));
opline.op1_type = IS_CONST; //opline->op1为 成员变量的值,result->u.op.var为fetch_simple_variable所获得的值
LITERAL_STRINGL(opline.op1, estrdup(CG(active_op_array)->vars[result->u.op.var].name), CG(active_op_array)->vars[result->u.op.var].name_len, );
CALCULATE_LITERAL_HASH(opline.op1.constant);
GET_POLYMORPHIC_CACHE_SLOT(opline.op1.constant); //class_node.op_type此时为IS_VAR
if (class_node.op_type == IS_CONST) {
opline.op2_type = IS_CONST;
opline.op2.constant =
zend_add_class_name_literal(CG(active_op_array), &class_node.u.constant TSRMLS_CC);
} else {
SET_NODE(opline.op2, &class_node);
}
GET_NODE(result,opline.result);
opline.extended_value |= ZEND_FETCH_STATIC_MEMBER;
opline_ptr = &opline; zend_llist_add_element(fetch_list_ptr, &opline);
}

zend_do_fetch_class 函数的运行

static int ZEND_FASTCALL  ZEND_FETCH_CLASS_SPEC_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
{
USE_OPLINE
zval *class_name = opline->op2.zv;
if (CACHED_PTR(opline->op2.literal->cache_slot)) {
EX_T(opline->result.var).class_entry = CACHED_PTR(opline->op2.literal->cache_slot);
} else {
EX_T(opline->result.var).class_entry = zend_fetch_class_by_name(Z_STRVAL_P(class_name), Z_STRLEN_P(class_name), opline->op2.literal + , opline->extended_value TSRMLS_CC);
CACHE_PTR(opline->op2.literal->cache_slot, EX_T(opline->result.var).class_entry);
//记住 这里被缓存住了

}
}
static int ZEND_FASTCALL zend_fetch_var_address_helper_SPEC_VAR_CONST(int type, ZEND_OPCODE_HANDLER_ARGS)
{
USE_OPLINE
zend_free_op free_op1;
zval *varname;
zval **retval; SAVE_OPLINE();
varname = _get_zval_ptr_var(opline->op1.var, execute_data, &free_op1 TSRMLS_CC); zend_class_entry *ce;
ce = CACHED_PTR(opline->op2.literal->cache_slot);
retval = zend_std_get_static_property(ce, Z_STRVAL_P(varname), Z_STRLEN_P(varname), , ((IS_VAR == IS_CONST) ? opline->op1.literal : NULL) TSRMLS_CC); }

找到成员变量

ZEND_API zval **zend_std_get_static_property(zend_class_entry *ce, const char *property_name, int property_name_len, zend_bool silent, const zend_literal *key TSRMLS_DC) /* {{{ */
{
zend_property_info *property_info;
ulong hash_value; hash_value = zend_hash_func(property_name, property_name_len+); //即使这里的类是子类,子类的ce->properties_info的数据复制于父类的properties_info这个hash table
if (UNEXPECTED(zend_hash_quick_find(&ce->properties_info, property_name, property_name_len+, hash_value, (void **) &property_info)==FAILURE)) {
if (!silent) {
zend_error_noreturn(E_ERROR, "Access to undeclared static property: %s::$%s", ce->name, property_name);
}
return NULL;
} if (UNEXPECTED(!zend_verify_property_access(property_info, ce TSRMLS_CC))) {
if (!silent) {
zend_error_noreturn(E_ERROR, "Cannot access %s property %s::$%s", zend_visibility_string(property_info->flags), ce->name, property_name);
}
return NULL;
} if (UNEXPECTED((property_info->flags & ZEND_ACC_STATIC) == )) {
if (!silent) {
zend_error_noreturn(E_ERROR, "Access to undeclared static property: %s::$%s", ce->name, property_name);
}
return NULL;
} if (UNEXPECTED(CE_STATIC_MEMBERS(ce) == NULL) ||
UNEXPECTED(CE_STATIC_MEMBERS(ce)[property_info->offset] == NULL)) {
if (!silent) {
zend_error_noreturn(E_ERROR, "Access to undeclared static property: %s::$%s", ce->name, property_name);
}
return NULL;
} return &CE_STATIC_MEMBERS(ce)[property_info->offset];
}

php类 静态变量赋值 static $name="abc"的更多相关文章

  1. PHP 类属性 类静态变量的访问

    php的类属性其实有两种,一种是类常量,一种是类静态变量.两种容易引起混淆. 如同静态类方法和类实例方法一样,静态类属性和实例属性不能重定义(同名),但静态属性可以和类常量同名. <?php c ...

  2. python类静态变量

    python的类静态变量直接定义在类中即可,不需要修饰符,如: 1 class Test: stc_attr = 1 def __init__(self,attr1,attr2): self.attr ...

  3. Spring/Spring boot中静态变量赋值

    情形1:静态变量为自动注入的对象 解决方案:设置两个变量,非静态变量使用@resource注入Bean,然后使用@PostConstruct在Spring初始化Bean成功后为静态变量赋值 @Comp ...

  4. c#静态变量赋值问题

    class Program { static int i = getNum(); int j = getNum(); ; static int getNum() { return num; } sta ...

  5. oc 基本语法 类 静态变量 常量

    // // ReViewClass.h // hellowWorld // 本类是oc复习练手类 // Created by hongtao on 2018/3/26. // Copyright © ...

  6. 静态变量符 static

    1.private是访问权限限定,static表示不要实例化就可以使用. (1)被static修饰的变量,叫静态变量或类变量,没有被static修饰的变量,叫实例变量. 对于静态变量在内存中只有一个拷 ...

  7. C++类静态变量的一种使用特例

    不同进程里的数据默认情况下是互不影响的. 静态变量是属于类本身的,它的所有实例可以共享这个静态变量,但是有个先天条件就是在同一个进程的情况下!!

  8. php 类静态变量 和 常量消耗内存及时间对比

    在对类执行100w次循环后, 常量最快,变量其次,静态变量消耗时间最高 其中: 常量消耗:101.1739毫秒 变量消耗:2039.7689毫秒 静态变量消耗:4084.8911毫秒 测试代码: cl ...

  9. [C/C++] 静态变量赋值问题 undefined reference to

    刚才在写代码的时候 用到了一个静态变量 然后在别人地方直接使用的时候 也就是 NetWork::Flag = 0; 像是这样使用的时候一直提示 undefined reference to 各种检查之 ...

随机推荐

  1. Cassandra1.2文档学习(16)—— 模式的变化

    参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/dml/dml_schema ...

  2. 从 Typecho 自定义字段的调用代码看去

    千呼万唤,Typecho 的"自定义字段"功能终于在 0.9 中出来了.然而,多数人还蒙在这样一个鼓里--该怎么在模板调用已经设置好的自定义字段呢?让我们从这里开始说下去: Typ ...

  3. wampsever在win10中安装扩展掉坑

    1.必须要退出wampserver 2.php pecl + 3.wampserver 64 3.0.6

  4. EditorLineEnds.ttr 受影响的D版本 Delphi 8-2010

    http://stackoverflow.com/questions/25295980/delphi-2006-2010-error-cannot-create-file-c-users-admin- ...

  5. C#网络通信

    Server: using System.Net; using System.Net.Sockets; using System; using System.Collections.Generic; ...

  6. Oracle 执行计划说明

    生成SQL的执行计划是Oracle在对SQL做硬解析时的一个非常重要的步骤,它制定出一个方案告诉Oracle在执行这条SQL时以什么样的方式访问数据:索引还是全表扫描,是Hash Join还是Nest ...

  7. PySide 简易教程<一>-------Hello PySide

    PySide 是一个python绑定的跨平台GUI Qt库.目前,支持Python的Qt库有两个PyQt和PySide.PySide是一个免费的软件,与PyQt不同之处在于使用了LGPL,允许PySi ...

  8. C语言-创建链表及排序

    #include <stdio.h> #define NEWNODE (Node *)malloc(sizeof(Node)) typedef struct mynode{ int num ...

  9. PHP 一个可以过滤非法脚本的函数

    这里提供一个过滤非法脚本的函数: function RemoveXSS($val) {     // remove all non-printable characters. CR(0a) and L ...

  10. Oracle的AUTOTRACE功能

    ORACLE9i在使用autotrace之前,需要作一些初始设置: 1.用sys用户运行脚本utlxplan.sql创建PLAN_TABLE表 脚本目录:(UNIX:$ORACLE_HOME/rdbm ...