Home > Backend Development > PHP Tutorial > Some understanding of PHP $this variable_PHP tutorial

Some understanding of PHP $this variable_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:49:32
Original
866 people have browsed it

This article is intended to introduce some understanding of the PHP $this variable to fellow students. I hope these articles will be helpful to you.


An interesting little example from the manual.
http://www.php.net/manual/zh/language.variables.basics.php

The code is as follows Copy code
 代码如下 复制代码

 $this = 'text'; // error
 $name = 'this';
 $$name = 'text'; // sets $this to 'text'

 echo $$name;

$this = 'text'; // error

$name = 'this';

$$name = 'text'; // sets $this to 'text'

echo $$name;

During PHP's lexical analysis, the $this variable conforms to its rules. When syntax parsing generates intermediate code, the PHP kernel will determine whether it is a $this variable based on the variable type when generating the assigned intermediate code. If so, then Report an error. Why is there an error reported here? Because this is a special variable, this variable will be added to the active symbol table when the object's member methods are initialized.

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


When a method is called inside a class definition, there is a pseudo variable $this available. $this is a reference to the calling object (usually the object the method belongs to, but possibly another object if called statically from a second object).

During lexical analysis, syntax analysis and generating intermediate code, $this exists as a special variable. Especially when generating intermediate code, the code is full of special processing for this. These are all preparations for subsequent operations, such as identifying and marking the use of this variable somewhere. There is a special variable this_var in the zend_op_array structure that stores opcode to identify whether there is a this variable. A function or a class method will generate a new zend_op_array. When generating intermediate code, it is judged whether the current variable is this variable.


This variable will have two states of existence during execution. One is the globally passed state, which is stored in EG(This). The other is the current scope state, which is stored in EG(active_symbol_table) (currently The execution environment's active symbol table).
When we execute an op_array, such as an object method, the PHP kernel will generate a zendexecutedata for this op_array. When generating initialization, EG(This) will be added to EG(active_symbol_table).

During the method call, if this variable is used, the value of EG (active_symbol_table) will be taken directly.

So where is EG(This) in an object initialized?
 代码如下 复制代码
 class Foo {
      public $var = 10;
 
      function t() {
           echo $this->var;    
      }
 
      function t2() {
       echo 33;
  }
 }
 
 $foo = new Foo();
 $foo->t();
As for the EG(This) variable itself, when we initialize the PHP execution environment, it will be initialized to NULL like other global variables (such as EG(scope), etc.). For an object, when we create an object and call it, the PHP kernel will directly assign the currently obtained object to EG(This), and the currently obtained object is the object itself that was created when the object was generated through the new operation. . Here’s a simple example:
The code is as follows Copy code
class Foo { Public $var = 10; function t() { echo $this->var; } Function t2() { echo 33; } } $foo = new Foo(); $foo->t();

The intermediate code generated by its main program flow 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                                                    :1
          3      DO_FCALL_BY_NAME                              0         
          4      ASSIGN                                                   !0,
   16     5      ZEND_INIT_METHOD_CALL                                    !0, 't'
          6      DO_FCALL_BY_NAME                              0         
          7    > RETURN   
                                               1this

The original object value of the variable is born in opcode NEW. After assignment (ASSIGN), the variable itself is passed to the caller of the execution environment during method initialization, and the caller passes the variable when executing the call (DO_FCALL_BY_NAME). For EG(This), when the op_array of this method is executed, when the environment of the current scope (zend_execute_data) is initialized, EG(This) will be added to the active symbol table as a $this variable, and the use of the $this variable in subsequent methods It will directly take the variables of the symbol table.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632703.htmlTechArticleThis article introduces some understanding to the students about the PHP $this variable. I hope these articles will be helpful to you. help. An interesting little example from the manual. http://www.php.ne...
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template