Home > Backend Development > PHP Tutorial > Detailed explanation of static member variables of classes_PHP tutorial

Detailed explanation of static member variables of classes_PHP tutorial

WBOY
Release: 2016-07-13 17:47:47
Original
846 people have browsed it

First of all, let’s talk about the difference between static variables and global variables. Global variables can be accessed by all functions. If multiple independent functions use variables with the same name, it will cause conflicts. When only one function needs to access a certain variable, We should use static variables instead of global variables

Static members in a class belong to the entire class rather than to an instance of the class. Static members only retain one variable value, and this variable value is valid for all instances. All instances share this member.

$this represents the current instance of the class, Self:: represents the class itself. Code outside the class cannot use this operator, and it cannot identify its position in the inheritance tree hierarchy. That is to say, when using self scope in an extended class, self can call methods declared in the base class, but it always calls methods that have been overridden in the extended class. The base class method is overridden, but if you want to call the base class method, you need to use the parent keyword.

Static members can also only belong to the parent class. A certain member is defined in the parent and child classes. You need to use parent to access the static members in the parent class. In this case, the static member values ​​saved in the parent and child classes are different.

Using a static method to call methods in a class can omit the code for instantiating the class, and it will be more efficient (eliminating a small part of the resources that need to be consumed when instantiating the class)


Lexical and Grammatical Analysis Process

1 In the file Zend/zend_compile.c, add two lines of code after the lexical analysis to print out the specific word segmentation derived from the lexical analysis

retval = lex_scan(&zendlval->u.constant TSRMLS_CC); //Original lexical analysis script

if(zendlval->u.constant.value.lval && zendlval->u.constant.value.lval< 10000)

printf("word: %ldn", zendlval->u.constant.value.lval);

else if(zendlval->u.constant.value.str.val)

printf("word: %sn", zendlval->u.constant.value.str.val);

2 Print out the case conditions of the lexical link in the lexical analysis zend_language_scanner.c to facilitate subsequent tracking

printf("word-line:%dn", yy_act);

switch (yy_act)

3 Print out the case conditions of the syntax link in the syntax analysis zend_language_parser.c to facilitate subsequent tracking

YY_REDUCE_PRINT(yyn);

printf("grammar-line: %dn", yyn);

switch(yyn)

4 Construct a class file containing static

class classname{

public static $valname = 'test';

public $val = 'test1';

function functionname(){

echo self::$valname;

}

}

$obj = new classname();

echo $obj->functionname();

5 Important Fragment Analysis

word: classname

grammar-line: 93

grammar-line: 86 

Gone zend_do_begin_class_declaration

Information of initialization class

And register the class into CG(class_table)

Set the current class to CG(active_class_entry)

Finally took out the next opcode and set it up

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

Remove this function zend_do_verify_access_types

After testing, static cannot be used to limit member variables of abstract classes, etc.

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 was called

Through CG (active_class_entry) detection, the interface cannot add member variables

Variables cannot be abstract or final

Check whether duplicate definitions have been made

Call zend_declare_property_ex

Determine whether it is a static variable through access_type& ZEND_ACC_STATIC

If it is a static variable, target_symbol_table= &ce->default_static_members;

If not then target_symbol_table= &ce->default_properties;

Finally, zend_hash_update updates the attribute field

6 Conclusion

The ordinary variables and member variables of the class are stored in the attributes of the class structure, only the target hash table stored in them is different

Excerpted from xiaoq3406’s column

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478469.htmlTechArticleFirst let’s talk about the difference between static variables and global variables. Global variables can be accessed by all functions. If there are multiple independent If a function uses a variable with the same name, it will cause a conflict. When there is only one...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template