Weak type PHP implementation principle

$a = 123;
$b = '456';

var_dump($a); // int(123)
var_dump($b); // string(3) "456"

A small frog: Students, today we share the contents are: realization of the principle PHP weakly typed, on top of the code we've written many times, do not know if you have doubt, when the output of it is to know how $ a purely digital , and $ b is a string?

A small frog: This is the type of weak features of PHP, then the weak type is how to achieve it, we first look at the underlying PHP variables to achieve

typedef struct _zval_struct zval;
...
struct _zval_struct {
    zvalue_value value; /* 储存变量的值 */
    zend_uint refcount__gc; /* 引用计数,用于GC */
    zend_uchar type; /* 储存变量类型 */
    zend_uchar is_ref__gc; /* 是否为引用,即 &$a */
};

A Ray: I'm sorry teacher, interrupt, this is what the language, we seem to know you from somewhere - you remember?

A small frog: Yes, you've seen, this is the famous C language, PHP is to use the C language, there is always an unknown language behind every successful language -

A Ray: teacher, I understand, type the record type of the variable, but this stuff really zvalue_value stored value of the variable

A small frog: the answer is correct, but the problem again, we know, but strongly typed language C, 123 and '456' in zvalue_value in is how to store it?

A Ray: This ...

甲小浩:Talk is cheap,show me the code

typedef union _zvalue_value {
    long lval; /* 长整型 */
    double dval; /* 浮点型 */
    struct {
        char *val;
        int len; /* 字符串的长度 */
    } str; /* 字符串 */
    HashTable *ht; /* 哈希 */
    zend_object_value obj; /* 对象 */
} zvalue_value;

A Ray: This is what the hell?

A small frog: This is a data type in the C language - Community: Allows you to different memory addresses the same storage data type, you can define a variety of data types, but any time there can be only one member with a value .

A Ray: suddenly the fog, finally understand PHP is weakly typed is how to achieve, and wonderful, really wonderful. However, I found inside the absence of members of type bool, nor storage array's members, how is it?

A small frog: Very good question, because bool value only true and false, can be understood as 1 and 0, then long can also be used to store the value of the bool; in addition, PHP array is stored in the HashTable, how to achieve specific you can check the following information

A small frog: the way to add a little bit of content, a lot of students at the time of writing, I will write

$a = 'abcdefg';
$alen = strlen($a);
for ($i = 0; $i < $alen; $i++) {
    // do something
}

$b = [1, 2, 3, 4, 5, 6];
$bsize = count($b);
for ($j = 0; $j < $bsize; $j++) {
    // do something
}

A small frog: fear of calling strlen frequently and count method will affect the performance of the program, because every time they have to calculate the length ...... but in fact just the opposite effect, looked PHP variables to achieve, we know that calling strlen and only when the count acquired len corresponding values ​​hashtable and size of it, and did not go to recalculate the length and size (this is similar to the implementation in many high-level language), we create a new variable to store the size and instead len We need to open up a new space to store them, wasting memory, more harm than good

A small frog: incidentally, to add a little content, in order to lay down the maximum community of members, namely: zend_object_value, community space occupies 24 bytes in the 64-bit system, zend_object_value occupies 16 bytes, and if a bool value is stored, still will open up a lot of memory, it has been optimized to achieve here in PHP7 in favor of the way the pointer is not directly stored in the object itself, so put the space community is reduced, and this is PHP7 quickly become an important reason .

A small frog: This concludes today's share! See you ~

Released three original articles · won praise 3 · Views 135

Guess you like

Origin blog.csdn.net/epwqgdnbrh/article/details/104749714