PHP Source code Analysis-Weak type variable implementation _php Tutorial

Source: Internet
Author: User
Tags php source code
PHP is a weakly typed, dynamic language script. When declaring a variable, it is not necessary to indicate the type of data it holds.

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, which can be changed during run time, and do not need to declare variable types before they are used.


So, question one, how does the Zend engine use C to implement this weak type?
In fact, the variables declared in PHP are stored in the ze with struct zval.

First we 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, you can see that _zvalue_value is a key part of really saving data. A weakly typed variable declaration implemented by a common body

Question two, how does the Zend engine identify and store multiple data types in PHP?
_zval_struct.type stores the true type of a variable, choosing how to get the value of Zvalue_value, depending 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
#de Fine Is_array 4
#define IS_OBJECT 5
#define Is_string 6
#define Is_resource 7
#define Is_constant 8
#d Efine 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
Look at a simple example: www.2cto.com
!--? php
$a = 1;
//At this time Zval.type = Is_long, then zval.value to fetch lval.
$a = array ();
//At this time Zval.type = Is_array, then zval.value to take ht.
!--? php
$a = 1;
//At this time Zval.type = Is_long, then zval.value to fetch lval.
$a = array ();
//At this time Zval.type = Is_array, then zval.value to take ht. The
is the most complex of these, and the "resource type" is often used in developing third-party extensions.
in PHP, any variable that is not part of the built-in variable type of PHP is considered a resource to be saved.
For example: Database handle, open file handle, open socket handle.

The resource type needs to be registered using the API functions provided by Ze, and the Declaration and use of the resource variables will be described in detail in a separate contents.

It is precisely because ze this way, so that PHP to achieve a weak type, and for ze, it is always facing the same type of zval

Excerpt from God's blog

http://www.bkjia.com/PHPjc/478520.html www.bkjia.com true http://www.bkjia.com/PHPjc/478520.html techarticle PHP is a weakly typed, dynamic language script. When declaring a variable, it is not necessary to indicate the type of data it holds. For 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.