PHP source code analysis-implementation of weak type variables-PHP Tutorial

Source: Internet
Author: User
PHP source code analysis-implementation of weak type variables. PHP is a weak type, dynamic language script. When declaring a variable, you do not need to specify the data type it stores. Example :? Php $ var1; $ varvariable; $ var1.00; $ var PHP is a weak type, dynamic language script. When declaring a variable, you do not need to specify the data type it stores.

For example:

$ Var = 1;
$ Var = "variable ";
$ Var = 1.00;
$ Var = array ();
$ Var = new Object ();
$ Var = 1;
$ Var = "variable ";
$ Var = 1.00;
$ Var = array ();
$ Var = new Object ();
Dynamic variables can be changed during running, and do not need to declare the variable type before use.


So, Question 1: How does the Zend Engine use C to implement this weak type?
In fact, the variables declared in PHP are saved using the zval struct in ZE.

First, let's open Zend/zend. h to see the definition of zval:


Typedef struct _ zval_struct zval;

Struct _ zval_struct {
/* Variable information */
Zvalue_value value;/* value */
Zend_uint refcount _ gc;
Zend_uchar type;/* active type */
Zend_uchar is_ref _ gc;
};

Typedef union _ zvalue_value {
Long lval;/* long value */
Double dval;/* double value */
Struct {
Char * val;
Int len;
} Str;
HashTable * ht;/* hash table value */
Zend_object_value obj;
} Zvalue_value;
Typedef struct _ zval_struct zval;

Struct _ zval_struct {
/* Variable information */
Zvalue_value value;/* value */
Zend_uint refcount _ gc;
Zend_uchar type;/* active type */
Zend_uchar is_ref _ gc;
};

Typedef union _ zvalue_value {
Long lval;/* long value */
Double dval;/* double value */
Struct {
Char * val;
Int len;
} Str;
HashTable * ht;/* hash table value */
Zend_object_value obj;
} Zvalue_value;
Zend/zend_types.h:


Typedef unsigned char zend_bool;
Typedef unsigned char zend_uchar;
Typedef unsigned int zend_uint;
Typedef unsigned long zend_ulong;
Typedef unsigned short zend_ushort;
Typedef unsigned char zend_bool;
Typedef unsigned char zend_uchar;
Typedef unsigned int zend_uint;
Typedef unsigned long zend_ulong;
Typedef unsigned short zend_ushort;
From the code above, we can see that _ zvalue_value is the key part of truly saving data. Declaration of weak type variables implemented through the shared body

Question 2: How does the Zend Engine identify and store various data types in PHP?
_ Zval_struct.type stores the real type of a variable and selects how to obtain the value of zvalue_value based on the type.

Type value list (Zend/zend. h ):
# Define IS_NULL 0
# Define IS_LONG 1
# Define IS_DOUBLE 2
# Define IS_BOOL 3
# Define IS_ARRAY 4
# Define IS_OBJECT 5
# Define IS_STRING 6
# Define IS_RESOURCE 7
# Define IS_CONSTANT 8
# Define IS_CONSTANT_ARRAY 9
Type value list (Zend/zend. h ):
# Define IS_NULL 0
# Define IS_LONG 1
# Define IS_DOUBLE 2
# Define IS_BOOL 3
# Define IS_ARRAY 4
# Define IS_OBJECT 5
# Define IS_STRING 6
# Define IS_RESOURCE 7
# Define IS_CONSTANT 8
# Define IS_CONSTANT_ARRAY 9
Let's look at a simple example: www.2cto.com
$ A = 1;
// In this case, zval. type = IS_LONG, and zval. value is used to retrieve lval.
$ A = array ();
// At this time, zval. type = IS_ARRAY, then zval. value is used to get ht.
$ A = 1;
// In this case, zval. type = IS_LONG, and zval. value is used to retrieve lval.
$ A = array ();
// At this time, zval. type = IS_ARRAY, then zval. value is used to get ht.
This is the most complex one, and "resource type" is often used in third-party extension development ".
In PHP, any variable that does not belong to the built-in variable type of PHP will be considered as a resource for saving.
For example, database handle, opened file handle, and opened socket handle.

Resource type, which must be registered using the API functions provided by ZE. the declaration and use of resource variables are described in a separate article.

It is precisely because of the processing method like ZE that PHP implements a weak type, and for ZE, it will always face the same type of zval.

From God's blog

Bytes. When declaring a variable, you do not need to specify the data type it stores. Example :? Php $ var = 1; $ var = variable; $ var = 1.00; $ var...

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.