PHP是弱類型,動態語言指令碼。在申明一個變數的時候,並不需要指明它儲存的資料類型。
例如:
$var = 1;
$var = "variable";
$var = 1.00;
$var = array();
$var = new Object();
$var = 1;
$var = "variable";
$var = 1.00;
$var = array();
$var = new Object();
動態變數,在運行期間是可以改變的,並且在使用前無需聲明變數類型。
那麼,問題一、Zend引擎是如何用C實現這種弱類型的呢?
實際上,在PHP中聲明的變數,在ZE中都是用結構體zval來儲存的。
首先我們開啟Zend/zend.h來看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;
從上述代碼中,可以看到_zvalue_value是真正儲存資料的關鍵區段。通過共用體實現的弱類型變數聲明
問題二、Zend引擎是如何判別、儲存PHP中的多種資料類型的呢?
_zval_struct.type中儲存著一個變數的真正類型,根據type來選擇如何擷取zvalue_value的值。
type值列表(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值列表(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
來看一個簡單的例子:www.2cto.com
$a = 1;
//此時zval.type = IS_LONG,那麼zval.value就去取lval.
$a = array();
//此時zval.type = IS_ARRAY,那麼zval.value就去取ht.
$a = 1;
//此時zval.type = IS_LONG,那麼zval.value就去取lval.
$a = array();
//此時zval.type = IS_ARRAY,那麼zval.value就去取ht.
這其中最複雜的,並且在開發第三方擴充中經常需要用到的是"資源類型".
在PHP中,任何不屬於PHP的內建的變數類型的變數,都會被看作資源來進行儲存。
比如:資料庫控制代碼、開啟的檔案控制代碼、開啟的socket控制代碼。
資源類型,需要使用ZE提供的API函數來註冊,資源變數的聲明和使用將在單獨的篇目中進行詳細介紹。
正是因為ZE這樣的處理方式,使PHP就實現了弱類型,而對於ZE的來說,它所面對的永遠都是同一種類型zval
摘自 God's blog
http://www.bkjia.com/PHPjc/478520.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478520.htmlTechArticlePHP是弱類型,動態語言指令碼。在申明一個變數的時候,並不需要指明它儲存的資料類型。 例如: ?php $var = 1; $var = variable; $var = 1.00; $var...