PHP $ this variable comprehension _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP $ this variable for some understanding. This article is intended to help you understand the PHP $ this variable. An interesting small example in the manual. Www. php. ne this article is intended to help you understand the PHP $ this variable.


An interesting small example in the manual.
Http://www.php.net/manual/zh/language.variables.basics.php

The code is as follows:

$ This = 'text'; // error
$ Name = 'eas ';
$ Name = 'text'; // sets $ this to 'text'

Echo $ name;

During PHP lexical analysis, the $ this variable complies with its rules. when syntax parsing generates intermediate code, the PHP kernel determines whether the value is $ this variable based on the variable type when generating the intermediate code of the value assignment. If yes, an error is returned. Why is an error reported here? Because this is a special variable, this variable is added to the table of activity symbols during initialization of object member methods.

In the Member methods of the class, you can use the-> (object operator): $ this-> property (where property is the property name) method to access non-static properties.

When a method is called inside the class definition, there is an available pseudo variable $ this. $ This is a reference to the main object (usually the object subordinate to this method, but it may also be another object when it is called from the second object statically ).

When lexical analysis, syntax analysis, and intermediate code generation, $ this is a special variable, especially when the intermediate code is generated, the code is filled with special processing for this. These are all preparations for subsequent operations. for example, to identify and mark the use of this variable somewhere, there is a variable this_var in the zend_op_array structure storing opcode to identify whether this variable exists. A function or a class method will generate a new zend_op_array. when the intermediate code is generated, it will determine whether the current variable is this variable.

This variable may exist in two states during execution. one is the state of global transfer, which is stored in EG (This), and the other is the state of the current scope, store this variable in EG (active_symbol_table) (the active symbol table in the current execution environment ).
When we execute an op_array, such as an object method, the PHP kernel will generate a zendexecutedata for This op_array. during initialization, EG (This) will be added to EG (active_symbol_table ).
If this variable is used during method calling, the values of EG (active_symbol_table) are taken directly.

So where can I initialize the EG (This) in an object?
For example (This) variable itself, when we initialize the PHP execution environment, it will be initialized to NULL like other global variables (such as EG (scope.
For an object, when we create an object and call it, the PHP kernel will directly assign the obtained object to EG (This ), the currently obtained object is the object itself created when an object is generated through the new operation.

The following is a simple example:

The code is as follows:
Class Foo {
Public $ var = 10;

Function t (){
Echo $ this-> var;
}

Function t2 (){
Echo 33;
}
}

$ Foo = new Foo ();
$ Foo-> t ();

The intermediate code generated by the main program flow is as follows:

The code is as follows:
Function name: (null)
Number of ops: 8
Compiled vars :! 0 = $ foo
Line # * op fetch ext return operands
---------------------------------------------------------------------------------
2 0> NOP
15 1 ZEND_FETCH_CLASS 4: 1 'foo'
2 NEW $2: 1
3 DO_FCALL_BY_NAME 0
4 ASSIGN! 0, $2
16 5 ZEND_INIT_METHOD_CALL! 0,'t'
6 DO_FCALL_BY_NAME 0
7> RETURN
1 this

The original object value of the variable is generated in opcode NEW. after being assigned a value (ASSIGN), the variable itself is passed to the caller of the execution environment during method initialization, when the caller executes the call (DO_FCALL_BY_NAME), the variable is passed to EG (This). when the op_array of This method is executed, the current scope environment (zend_execute_data) is initialized, the EG (This) will be added to the activity symbol table as the $ this variable. the use of the $ this variable in the subsequent method will directly take the variable of the symbol table.

I hope some articles will help you understand the variable $ this. An interesting small example in the manual. Http://www.php.ne...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.