"PHP kernel Learning" variables and data types
|=-----------------------------------------------------------------------=||=---------------------=[The variables and data types in the PHP kernel]=--------------------=||=-----------------------------------------------------------------------=||=--------------------------=[by D4shman]=-----------------------------=||=-----------------------------------------------------------------------=||=-------------------------=[May 6,]=---------------------------=||=-----------------------------------------------------------------------=|
(_____ \| | | (_____ \ /\ / _____) | / )_____) ) |__ | |_____) ) / \ | / | | / /| ____/| __)| (_____ ( / /\ \| | | |< <| | | | | | | | |__| | \_____| | \ \|_| |_| |_| |_|______|\______)_| \_) (Salute to Phrack!)
<--------------------------(Table of Contents)-------------------------->
The structure and type of the 0x01 variable0x02 Hash Table--php the Soul0x03 Constants0X04 Reference Documents<------------------------------------------------------------------------->
/////The structure and type of the 0x01 variable/////1. Data type1.1 Static type language (C/java), compile-time determination1.2 Dynamic type language (Php/python), run-time determination1.3 Untyped language (assembly), the underlying storage of operations
All variables in the 2.php kernel are saved using the same data structure zval, which also represents the various data types in PHP, which contain not only the value of the variable, but also the type of the variable. This is the core of PHP's weak type.8 data types in PHP:2.1 Scalar Type: Boolean, Integer, float, string2.2 Composite Type: Array, object2.3 Special Type: resource, NULL 3.zval structure (defined in PHP source directory zend/zend.h):struct _zval_struct{/*variable information*/Zvalue_value value/*value, variable values */Zend_uint refcount__gc/*reference count, reference counter */Zend_uchar types/*active Type, variable/*Zend_uchar is_ref__gc; /* Whether the variable is referenced */}
4. Variable type:/*data Types */#define Is_null0#define Is_long 1#define Is_double 2#define Is_bool 3#define Is_array4#define Is_object5#define Is_string 6#define Is_resource7#define Is_constant8#define Is_constant_array9#define Is_callable10 5. The value of the variable is storedtypedef Union _ZVALUE_VALUE {Long lval; /*long, BOOL, resource type */Double dval;/*double type */struct {/*string type, Len holds the length of the string */Char *val;int Len;} str;HashTable *ht; /* array, implemented with Hashtable */Zend_object_value obj; /*object Type */} Zvalue_value; The reason for this is that the Community (union) is because a variable can have only one type, which is consistent with the nature of the community, and it wastes memory if the struct is used. Example: Creating an integer variable with a value of 10 Lvar, in PHP script, is simple: $lvar = 10The implementation in the PHP kernel might look something like this:Zval lval;Z_type (Lvar) = Is_long;Z_lval (Lvar) = 10; /////0x02 Hash Table--php the Soul/////1. Why use a hash tableHash tables typically provide crud (Create, Read, Update, Delete) operations, and design a reasonable hash table with an O (1) time complexity, which is why it is beloved.Hash (key), index 2. The implementation of the hash table: the structure bucket and _hashtable constitute the complete hashtable.First look at the bucket structure (defined in ZEND/ZEND_HASH.H):typedef struct BUCKET {ULONG H; /*hash Value */Length of UINT Nkeylength;/*key */void *pdata;/* The address of the memory block to be saved, usually the address of malloc */Void *pdataptr;/* saves pointer data, without a pointer to malloc, to prevent memory fragmentation */struct bucket *plistnext; The next element in the/*bucket that has the same hash value */struct bucket *plistlast; The previous element in the/*bucket that has the same hash value */struct bucket *pnext;/* The next element of the doubly linked list */struct bucket *plast; /* Previous element of doubly linked list */const char *arkey;/* save key*/} buckets; It can be seen that bucket is a doubly linked list, which is to solve the problem of multiple key conflicts (i.e., the link method in the introduction of algorithms) Look again at the _hashtable structure:typedef struct _HASHTABLE {UINT Ntablesize; The size of the/*bucket array */UINT Ntablemask;Number of elements in the UINT nnumofelements;/*hashtable */ULONG nnextfreeelement;/* Next available bucket position */Bucket *pinternalpointer/* Traverse Hashtable element */Bucket *plisthead;/* bi-directional list header */Bucket *plisttail;/* Two-way list footer */Bucket **arbuckets;/*bucket Array */} HashTable; ========The structure of the Hashtable is shown here.========
3. The Magical digital--33See my original blog: http://blog.csdn.net/wusuopubupt/article/details/11479869Here is a comment in the PHP source code:/** DJBX33A (Daniel J. Bernstein, Times with addition)** This is Daniel J. Bernstein ' s popular ' times ' hash function as* Posted by him years ago on comp.lang.c. It basically uses a function* like ' hash (i) = hash (i-1) * + Str[i] ". The one of the best* Known hash functions for strings. Because It is both computed very* Fast and distributes very well.** The magic of number, i.e. why it works better than many other* constants, prime or not, have never been adequately explained by* anyone. So I try a explanation:if one experimentally tests all* Multipliers between 1 and [as RSE did now] one detects that even* Numbers is not useable at all. The remaining odd numbers* (except for the number 1) work more or less all equally well. They* All distribute on an acceptable the and this is a hash table* With an average percent of approx. 86%.** If One compares the chi^2 values of the variants, the number is not* even has the best value. But the number is and a few other equally* Good numbers like, 127 and 129 has nevertheless a great* Advantage to the remaining numbers in the large set of possible* Multipliers:their Multiply operation can be replaced by a faster* Operation based on just one shift plus either a single addition* or subtraction operation. And because a hash function have to both* Distribute good _and_ have to is very fast to compute, those few* Numbers should is preferred and seems to being the reason why Daniel J.* Bernstein also preferred it.***--Ralf S. Engelschall <[email protected]>*/
4. Operation Interface of hash table (partial parameter omitted)Initialize Hashtable:int _zend_hash_init (HashTable *ht, uint nSize, hash_func_t phashfunction);Add new hash value: int _zend_hash_add_or_update (HashTable *ht, const char *arkey, uint nkeylength, void *pdata)Find Hash:int zend_hash_find (const HashTable *HT, const char *arkey, uint nkeylength, void **pdata);
/////0x03 Constants/////1. Internal structure of constantstypedef struct _ZEND_CONSTANT {Zval value;int flags; /* Constant mark, such as Const_persistent | Const_cs * *Char *name;UINT Name_len;int module_number;} zend_constant;
2.define the process of defining constantsDefine implementation (defined in ZEND/ZEND_BUILTIN_FUNCTIONS.C), here is some of the core code: Zend_function (define){/* Check if constant name exists */if (ZEND_MEMNSTR (Name, "::", sizeof ("::")-1, name + Name_len)) {Zend_error (e_warning, "Class constants cannot be defined or redefined");Return_false;} ...//class constants defined here do not introduce C.value = *val;Zval_copy_ctor (&c.value);if (Val_free) {Zval_ptr_dtor (&val_free);}C.flags = case_sensitive; /* Case Sensitive */C.name = zend_strndup (name, Name_len);C.name_len = name_len+1;C.module_number = php_user_constant;if (Zend_register_constant (&c tsrmls_cc) = = SUCCESS) {/* Register constant */Return_true;} else {Return_false;}} 3. Magic ConstantsThe Magic constants in PHP are called constants, but their values actually change with their position in the code.The current line number in the __line__ file.The full path and file name of the __file__ file. If used in the included file, returns the file name that is included.The directory where the __dir__ file resides. If used in the included file, returns the directory where the included files are located. It is equivalent to DirName (__file__).__function__ function nameThe name of the __class__ class. The class name includes its declared action area (for example, Foo\bar).__trait__ TRAIT's name. The Trait name includes its declared function area (for example, Foo\bar).Method name of the __method__ class__NAMESPACE__ the name of the current namespace (case sensitive). This constant is defined at compile time (PHP 5.3.0 is new). Instead of parsing at run time, the PHP kernel replaces the content assignments of these constants in lexical parsing. As an example:
Echo __line__;Function Demo () {Echo __function__;}Demo ();?>PHP has replaced these constants with the corresponding values in lexical parsing, which can be seen in the following PHP code:
Echo 2;Function Demo () {echo "Demo";}Demo ();?>
===========This involves compiling the principle of knowledge, need to add.=========== /////0X04 Reference Documents/////
Tipi:http://www.php-internals.com/book/?p=chapt03/03-00-variable-and-data-types
-
1 floor wusuopubupt yesterday 14:06
-
This article GitHub address: Https://github.com/wusuopubupt/phpLib/blob/master/PHP%E5%86%85%E6%A0%B8%E4%B8%AD%E7%9A%84%E5%8F %98%e9%87%8f%e5%92%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b