Home > Backend Development > PHP Tutorial > Wang Shuai: In-depth PHP kernel (1) - Exploring the principle of weakly typed variables

Wang Shuai: In-depth PHP kernel (1) - Exploring the principle of weakly typed variables

WBOY
Release: 2016-08-08 09:21:11
Original
1054 people have browsed it

PHP is a simple and powerful language that provides many language features suitable for the Web, including weak typing of variables. Under the weak typing mechanism, you can assign any type of value to a variable.
PHP is executed through Zend Engine (hereinafter referred to as ZE). ZE is written in C and implements a set of weak type mechanisms at the bottom. ZE's memory management uses optimization strategies such as copy-on-write and reference counting to reduce memory copies when reassigning variables.

The following not only takes you to explore the principles of PHP weak typing, but also writes about PHP extensions and introduces how to operate PHP variables.

1. PHP variable types

There are 8 variable types in PHP:

  • Standard types: boolean, integer, floating point float, string
  • Complex types: array array, object
  • Special type: resource

PHP does not strictly check the variable type. Variables can declare their type without displaying it, and assign values ​​directly during runtime. Variables can also be converted freely. As in the following example, without implementation declaration, $i can be assigned any type of value.

[php] view plaincopy

  1. $i = 1; //int $i = 'show me the money'; //string $i = 0.02; //float $i = array( 1, 2, 3); // array $i = new Exception('test', 123); // object $i = fopen('/tmp/aaa.txt', 'a') // resource ?>

If you don’t have a deep understanding of the principle of weak typing, you will have “exceeding expectations” surprises when comparing variables.

[php] view plaincopy

  1. $str1 = null; $str2 = false; echo$str1 ==$str2 ? 'Equal' : 'Not equal'; $str3 = ''; $str4 = 0; echo $str3= =$str4 ? 'equal' : 'not equal'; $str5 = 0; $str6 = '0'; echo $str5==$str6 ? 'equal' : 'not equal'; ?>

All the above three results They are equal because PHP performs variable conversion internally when comparing variables. If you want the value and type to be determined at the same time, please use three = (for example, $a===0) to determine. Maybe you will find it commonplace, maybe you will find it amazing, then please join me to delve into the PHP kernel and explore the principle of PHP variables.

2. Introduction to variable storage and standard types

All variables in PHP are implemented with the structure zval. In Zend/zend.h we can see the definition of zval:

[php] view plaincopy

  1. typedef union _zvalue_value { long lval; /* long value */ double dval; /* dou ble value */ struct { char *val; int len; /* this will always be set for strings */ } str; /* string (always has length) */ HashTable *ht; /* an array */ zend_object_value obj; /* stores an object store handle, and handlers */}zvalue_value; means reference count
1
is_ref__gc valueThe value of the stored variableThe specific type of the variable
Indicates whether it is a reference 0
type

Among them, refcount__gc and is_ref__gc indicate whether the variable is a reference. The type field identifies the type of the variable. The value of type can be: IS_NULL, IS_BOOL, IS_LONG, IS_FLOAT, IS_STRING, IS_ARRAY, IS_OBJECT, IS_RESOURCE. PHP chooses how to store zvalue_value based on the type.
zvalue_value can realize the core of variable weak type, defined as follows:

[php] view plaincopy

  1. typedef union _zvalue_value { long lval; /* long value */ double dval; /* double value */ struct { char *val; int len; /* this will always be set for strings */ } str; /* string (always has length) */ HashTable *ht; /* an array */ zend_object_value obj; /* stores an object store handle, and handlers */ } zvalue_value;
Boolean type, zval.type=IS_BOOL, will read the zval.value.lval field, the value is 1/0. If it is a string, zval.type=IS_STRING will read zval.value.str, which is a structure that stores the string pointer and length.

In C language, use " plaincopy

typedefstruct_zend_rsrc_list_entry {    void *ptr;   int type;   int refcount; }zend_rsrc_list_entry; The final pointer to the implementation, such as a file handle or a database connection structure . type is a type tag used to distinguish different resource types. refcount is used for reference counting of resources.

In the kernel, the resource type is obtained through the function ZEND_FETCH_RESOURCE.

[php] view plaincopy

  1. ZEND_FETCH_RESOURCE(con, type, zval *, default, resource_name, resource_type);

5. Conversion of variable types

Follow Now what we know about the PHP language, variables The type depends on the zval.type field indication, and the content of the variable is stored in zval.value according to zval.type. When variables are needed in PHP, only two steps are required: change the value or pointer of zval.value, and then change the type of zval.type. However, for some of PHP's advanced variables Array/Object/Resource, variable conversion requires more operations.

Variable conversion principles are divided into 3 types:

5.1 Standard type mutual conversion

is relatively simple, just follow the above steps for conversion.

5.2 Standard type and resource type conversion

The resource type can be understood as int, which is more convenient for converting standard types. After conversion, the resource will be closed or recycled.

[php] view plaincopy

  1. $var = fopen('/tmp/aaa.txt', 'a' );// Resources #1 $var = (int) $var; var_dump($var); // Output 1 ?>

5.3 Standard type and complex type conversion

Array conversion int/floating point type Float will return the number of elements; conversion to bool will return whether there are elements in Array; conversion to string will return 'Array' and throw a warning.
Details depend on experience, please read the PHP manual: http://php.net/manual/en/language.types.type-juggling.php

5.4 Complex type mutual conversion

array and object can be converted to each other. If any other type of value is converted to an object, an instance of the built-in class stdClass will be created.

When we write PHP extensions, the PHP kernel provides a set of functions for type conversion:

void convert_to_long(zval* pzval)
void convert_to_double(zval* pzval)
void convert_to_long_base(zval* pzval, int base)
void convert_to_null(zval* pzval)
void convert_to_boolean(zval* pzval)
void convert_to_array(zval* pzval)
void convert_to_object( zval* pzval)
void convert_object_to_type(zval* pzval, convert_func_t converter)

A set of macros provided by the PHP kernel to conveniently access zval and obtain the value of zval in a more fine-grained manner:

Kernel access to zval container API
macro Access variables
Z_LVAL(zval) (zval).value.lval
Z_DVAL(zval) (zval).value.dval
Z_STRVAL(zval) (zval).value.str.val
Z_STRLEN(zval) (zval).value.str. len
Z_ARRVAL(zval) (zval).value.ht
Z_TYPE(zval) (zval).type
Z_ LVAL_P(zval) ( *zval).value.lval
Z_DVAL_P(zval) (*zval).value.dval
Z_STRVAL_P(zval_p) (*zval).value.str.val
Z_STRLEN_P(zval_p) (*zval).value.str.len
Z_ARRVAL_P(zval_p) (*zval).value.ht
Z_OBJ_HT_P(zval_p) (*zval).value.obj.handlers
Z_LVAL_PP(zval_pp) (**zval).value.lval
Z_DVAL_PP(zval_pp) (**zval ).value.dval
Z_STRVAL_PP(zval_pp) (**zval).value.str.val
Z_STRLEN_PP(zval_pp) (**zval).value.str. len
Z_ARRVAL_PP(zval_pp) (**zval). value.ht

6. Variable symbol table and scope

PHP’s variable symbol table and zval value mapping is through HashTable (hash table, also called hash table, hereinafter referred to as HT). HashTable is widely used in ZE, including Language features such as constants, variables, and functions are organized by HT, and the array type in PHP is also implemented through HashTable.
For example:

[php] view plaincopy The variable name of $var will be stored in the variable symbol table and represents $ The zval structure of the var's type and value is stored in a hash table. The kernel implements access to PHP variables through the hash mapping of the variable symbol table and the zval address.

Why do we need to mention scope? Because the internal variables of the function are protected. According to the scope, PHP variables are divided into global variables and local variables. Each scope PHP maintains a HashTable of symbol tables. When creating a function or class in PHP, ZE will create a new symbol table to indicate that the variables in the function or class are local variables. This achieves the protection of local variables - variables inside the function cannot be accessed from the outside. When creating a PHP variable, ZE will assign a zval, set the corresponding type and initial value, and add the variable to the symbol table of the current scope so that the user can use the variable.
    ZEND_SET_SYMBOL is used in the kernel to set variables:
  1. [php] view plaincopy ZEND_SET_SYMBOL(EG(active_symbol_table), "foo"
  2. , foo);


View _zend_executor_globals structure

[php] view plaincopy

  1. Zend/zend_globals.h struct _zend_executor_globals { ​HashTable *active_symbol_table;//Symbol table of local variables //Omitted };

When writing a PHP extension, you can access PHP's variable symbol table through the EG macro. EG (symbol_table) accesses the variable symbol table of the global scope, and EG (active_symbol_table) accesses the variable symbol table of the current scope. The local variable stores a pointer, which is passed to the corresponding function when operating on the HashTable.

In order to better understand the hash table and scope of variables, let’s take a simple example:
  1. [php] view plaincopy
  2. $temp =
  3. 'global'

;

function

test() {

$temp

=

    'active'
  1. ; } test(); var_dump($temp); ?> Create a variable $temp outside the function, which will be added to the global symbol table and allocated in the HashTable of the global symbol table A character zval with value 'global'. Create the internal variable $temp of the function test, add it to the symbol table belonging to the function test, and assign the character type zval with the value 'active'. 7. Variable operation in PHP extensionCreate PHP variablesWe can call the function MAKE_STD_ZVAL (pzv) in the extension to create a PHP callable variable. The macros applied to MAKE_STD_ZVAL are: [php] view plaincopy
    1. #define MAKE_STD_ZVAL(zv) ALLOC_ZVAL(zv);INIT_PZVAL(zv) #define ALLOC_ZVAL(z) ZEND_FAST_ALLOC (z, zval, ZVAL_CACHE_LIST) #define ZEND_FAST_ALLOC(p, type, fc_type) (p) = (type * ) emalloc(sizeof(type)) #define INIT_PZVAL(z) (z)->refcount__gc = 1;(z)->is_ref__gc = 0;

    MAKE_ After STD_ZVAL(foo) is expanded, we get:

    [php] view plaincopy

    1. (foo) = (zval *) emalloc(sizeof(zval)); (foo)->refcount__gc = 1; (foo)->is_ref__gc = 0;

    Can watch Out, MAKE_STD_ZVAL does three things: allocates memory, initializes refcount and is_ref in the zval structure.

    The kernel provides some macros to simplify our operations. You can set the type and value of zval in just one step.

    ZVAL_BOOL(pzv, 0);Z_TYPE_P(pzv) = IS_LONG;Z_LVAL_P(pzv) = l;Z_TYPE_P(pzv) = IS_DOUBLE;Z_LVAL_P(pzv) = d;Z_TYPE_P(pzv) = IS_STRING;Z_STRLEN_P (pzv) = len; {Z_STRVAL_P(pzv) = str;} Z_TYPE_P(pzv) = IS_RESOURCE;Z_RESVAL_P(pzv) = res;


    The dup parameter in ZVAL_STRINGL(pzv,str,len,dup)

    First explain ZVAL_STRINGL(pzv,str,len,dup); The two parameters str and len are easy to understand, because we know that they are saved in the kernel The address of the string and its length. The meaning of the following dup is actually very simple. It indicates whether the string needs to be copied. If the value is 1, a new memory will be allocated first and the string will be assigned, and then the address of the new memory will be copied to pzv. If it is 0, the address of str will be directly assigned to zval.

    The difference between ZVAL_STRINGL and ZVAL_STRING

    If you want to intercept the string at a certain position or already know the length of the string, you can use the macro ZVAL_STRINGL(zval, string, length, duplicate), which is explicitly specified String length instead of using strlen(). This macro takes the string length as argument. But it is binary safe and faster than ZVAL_STRING because there is one less strlen.
    ZVAL_RESOURCE is approximately equal to ZVAL_LONG

    In Chapter 4, we said that the value of the resource type in PHP is an integer, so ZVAL_RESOURCE works similarly to ZVAL_LONG, except that it sets the type of zval to IS_RESOURCE.

    8. Summary

    PHP’s weak typing is completed through ZE’s zval container conversion. Variable names and zval data are stored through hash tables, which has a certain sacrifice in operating efficiency. In addition, due to the implicit conversion of variable types, insufficient detection of variable types during the development process may cause problems.

    However, PHP’s language features such as weak typing, arrays, memory hosting, and extensions are very suitable for web development scenarios. The development efficiency is very high and it can speed up the product iteration cycle. In massive services, often the bottleneck lies in the data access layer, not the language itself. In actual use, PHP not only serves as the logic layer and presentation layer, we even use the UDPServer/TCPServer developed by PHP as the middle layer of data and cache.

    The above introduces Wang Shuai: In-depth PHP Core (1) - Exploring the Principle of Weakly Typed Variables, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

    API Macros for Accessing zval
    Macro implementation method
    ZVAL_NULL(pvz) Z_TYPE_P(pzv) = IS_NULL
    ZVAL_BOOL(pvz) Z_TYPE_P(pzv) = IS_BOOL;
    Z_BVAL_P(pzv) = b ? 1 : 0; VAL_FALSE(pvz)
    ZVAL_LONG(pvz, l)(l is the value)
    ZVAL_DOU BLE(pvz, d )
    ZVAL_STRINGL(pvz, str, len, dup)
    if (dup) { {Z_STRVAL_P(pzv) =estrndup(str, len + 1);} }else { }
    ZVAL_STRING(pvz, str, len) ZVAL_STRINGL(pzv, str,strlen(str), dup);




    ZVAL_RESOURCE(pvz, res)
Related labels:
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