The object of the principle of PHP (i)

Source: Internet
Author: User

    • Author: laruence ()
    • This address: http://www.laruence.com/2008/08/22/412.html
    • Reprint please indicate the source

Maybe you know, maybe you don't know that PHP is a weak type, dynamic scripting language. The so-called weak type, that is, PHP does not strictly verify the variable type (strictly speaking, PHP is a medium-strong type language, this part will be described in a later article), when declaring a variable, do not need to display the type of data that indicates it to save:

  1. <?php
  2. $var = 1; //int
  3. $var = "Laruence"; //string
  4. $var = 1.0002; //float
  5. $var = array(); //Array
  6. $var = new Exception(' error '); //object;

Dynamic language, that is, the language structure of PHP can be changed at runtime, for example, we require a function definition file at run time, which causes the dynamic change of the function table of the language.
The so-called scripting language, that is, PHP is not run independently, to run PHP we need the PHP parser:

    1. /usr/bin/php-f example.php

As I have already said in the previous article, PHP is executed through Zend engine (ZE, Zend Engine), ZE is written in C, and everyone knows that C is a strongly typed language, that is, all variables in C are declared to final destruction, only one type of data can be saved. So how does PHP implement the weak type on the basis of ze?

First of all, if you have not contacted the source code analysis of PHP before, expand the development. If you don't understand PHP's architecture and have not heard of Ze, then I suggest you look at my previous article, and especially recommend:

    • PHP (PHP internals)
    • Deep understanding of PHP principles opcodes

In PHP, all variables are stored in a structure-zval, and in zend/zend.h we can see the definition of Zval:

    1.   typedef struct Span class= "Sh_classname" >_zval_struct {
    2.     zvalue_value value
    3.     zend_uint RefCount
    4.     zend_uchar type
    5.     zend_uchar Is_ref
    6.   } zval
    7.  

Where Zvalue_value is a key part of really preserving data, now it's time to reveal the answer, how does PHP implement the weak type on ze basis? Because Zvalue_value is a consortium (union),

  1. typedef Union _zvalue_value {
  2. long lval;
  3. double dval;
  4. struct {
  5. Char *val;
  6. int len;
  7. } str;
  8. HashTable *ht;
  9. zend_object_value obj;
  10. } zvalue_value;

So how does this structure store multiple types of PHP?
The types of variables that are common in PHP are:

    1. 1. Integer/floating-point/Long integer/bool value, etc.
    2. 2. String
    3. 3. Array/associative array
    4. 4. Objects
    5. 5. Resources

PHP stores the true type of a variable based on the Type field in Zval, and then chooses how to get the value of the Zvalue_value based on the type, for example, Integer and bool values:

    1. Zval.type = is_long;//Shaping
    2. Zval.type = is_bool;//Boolean value

Go to fetch zval.value.lval, for bool value lval∈ (0|1);
If it is double precision, or float will fetch Zval.value's dval.
And if it's a string, then:

    1. Zval.type = is_string

This time, you will take:
Zval.value.str
And this is also a structure that has a C-cell string and the length of the string.

For arrays and objects, the type corresponds to Is_array, Is_object, respectively, zval.value.ht and obj.

In particular, resources, in PHP, resources are a very special variable, any variables that are not part of PHP's built-in variable types are considered as resources to save, such as database handles, open file handles, and so on. For resources:

    1. Type = Is_resource

This time, will go to fetch zval.value.lval, at this time the lval is an integer indicator, and then PHP will be based on this indicator in PHP built in a resource list to query the corresponding resources (this part of the content, I will open a separate article to introduce), at present, All you need to know is that the lval at this point is like an offset value corresponding to the resource list.

    1. zend_fetch_resource(Con, type, Zval *, default, Resource_name, Resource_type);

With this mechanism, PHP implements a weak type, because for ze, it always faces the same type, that is zval.
PS: The team will go out to building tomorrow, I think I should write something to my blog reader to kill the weekend before I go. It's easy to start today, and next time, I'll go further into PHP variables, scopes, and the copy on write and change on write mechanism of variables, to be continued ....

The object of the principle of PHP (i)

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.