First, let's talk about the differences between static variables and global variables. global variables can be accessed by all functions. If multiple independent functions use variables of the same name, a conflict may occur, when only one function needs to access a variable, we should first use static variables instead of global variables.
The static member in the class, which belongs to the entire class rather than an instance of the class. The static member only keeps one variable value, which is valid for all instances, all instances share this member.
$ This indicates the current instance of the class. Self: indicates that the code outside the class itself cannot use this operator, and it cannot recognize its position in the hierarchy of the inheritance tree. That is to say, when the self scope is used in the extension class, self can call the methods declared in the base class, but it always calls methods that have been rewritten in the extension class. The base class method is overwritten, but if you want to call the base class method, you need to use the parent keyword.
Static members can only belong to the parent class. A member is defined in the parent and child classes. You need to use parent to access static members in the parent class. In this case, the static member values stored in the parent and child classes are different.
Using static methods to call methods in a class can omit the code of the instantiated class and be more efficient (saving a small amount of resources required for instantiating the class)
Lexical syntax analysis process
1. Add two lines of code to the file Zend/zend_compile.c after lexical analysis to print the tokens produced by lexical analysis.
Retval = lex_scan (& zendlval-> u. constant TSRMLS_CC); // The original lexical analysis script
If (zendlval-> u. constant. value. lval & zendlval-> u. constant. value. lval <10000)
Printf ("word: % ld \ n", zendlval-> u. constant. value. lval );
Else if (zendlval-> u. constant. value. str. val)
Printf ("word: % s \ n", zendlval-> u. constant. value. str. val );
2. Print the case condition of the lexical link in the lexical analysis zend_language_scanner.c to facilitate subsequent tracking.
Printf ("word-line: % d \ n", yy_act );
Switch (yy_act)
3. Print the case condition of the syntax link in the syntax analysis zend_language_parser.c to facilitate subsequent tracking.
Yy_performance_print (yyn );
Printf ("grammar-line: % d \ n", yyn );
Switch (yyn)
4. Construct a static class file
<? Php
Class classname {
Public static $ valname = 'test ';
Public $ val = 'test1 ';
Function functionname (){
Echo self: $ valname;
}
}
$ Obj = new classname ();
Echo $ obj-> functionname ();
5. Analysis of important segments
Word: classname
Grammar-line: 93
Grammar-line: 86
Zend_do_begin_class_declaration
Initialization class information
And register the class into CG (class_table)
Set the current class to CG (active_class_entry)
Finally, the next operation code is extracted and set.
Grammar-line: 98
Grammar-line: 168
Public
Grammar-line: 183
Grammar-line: 181
Word: static
Grammar-line: 186
Z_LVAL (yyval). u. constant) = ZEND_ACC_STATIC;
Grammar-line: 182
The zend_do_verify_access_types function is used.
Static cannot be used to restrict abstract member variables.
Word: valname
Grammar-line: 177
Grammar-line: 169
Word: test
Grammar-line: 303
Grammar-line: 309
Grammar-line: 192
The zend_do_declare_property function is called.
It is detected by CG (active_class_entry). The member variables cannot be added to the interface.
The variable cannot be abstact or final.
Check for repeated Definitions
Call zend_declare_property_ex
Access_type & ZEND_ACC_STATIC determines whether it is a static variable.
If it is a static variable, target_symbol_table = & ce-> default_static_members;
If not, target_symbol_table = & ce-> default_properties;
Last, zend_hash_update updates the attribute field.
6 conclusion
The common variables and member variables of a class are stored in the class structure attributes, but they are stored in different target hash tables.
From the column xiaoq3406