Variables explored in PHP kernel, _PHP tutorial

WBOY
Release: 2016-07-12 09:02:36
Original
839 people have browsed it

Variables explored by PHP kernel,

PHP variable components:

Variable name: The variable name in PHP language starts with $, English/underscore, can contain numbers, underscores, letters, and is case-sensitive. At the same time, PHP also supports compound variables, in the shape of $$A, which increases the dynamics of PHP.

Type: PHP is a weakly typed language and can assign any type of value.

Content: There can only be one value at the same time.

There are 8 data types in the php language, divided into three major categories:

1. Scalar type: Boolean, integer, float, string;

2. Composite type: object, array;

3. Special types: NULL, resource;

PHP is a weakly typed language. All variables within the implementation store data through the structure zval, which not only includes the value of the variable, but also the type of the variable, which is the core of PHP's weak type.

zval data structure:

struct _zval_struct{
  zvalue_value value;    //存储变量的值
  zend_unint  refcount_gc; //引用计数
  zend_char  is_ref_gc;  // 是否为引用
  zend_char  type;     //存储变量的类型
}
Copy after login

Zvalue_value is not a structure. In order to save memory, it is implemented using union, because the variable can only represent one type at the same time. Its prototype:

typedef union _zvalue_value{
  long lval;         
  double dval;
  struct {
      char *val;
      int len;      //字符串的长度
    }str;
  HashTable *ht;       //保存数组
  zend_object_value obj;   //对象
}zvalue_value;

Copy after login

Hash table:

Many implementations inside PHP are based on hash tables: variable scopes, function tables, class attributes, methods, etc. Many data inside the Zend engine are stored in hash tables.

PHP arrays use hash tables to store associated data. The hash table implementation uses two data structures, HashTable and Bucket:

HashTable:

typedef struct _hashtable { 
  uint nTableSize;    // hash Bucket的大小,最小为8,以2x增长。
  uint nTableMask;    // nTableSize-1 , 索引取值的优化
  uint nNumOfElements;  // hash Bucket中当前存在的元素个数,count()函数会直接返回此值 
  ulong nNextFreeElement; // 下一个数字索引的位置
  Bucket *pInternalPointer;  // 当前遍历的指针(foreach比for快的原因之一)
  Bucket *pListHead;     // 存储数组头元素指针
  Bucket *pListTail;     // 存储数组尾元素指针
  Bucket **arBuckets;     // 存储hash数组
  dtor_func_t pDestructor;  // 在删除元素时执行的回调函数,用于资源的释放
  zend_bool persistent;    // 指出了Bucket内存分配的方式。如果persisient为TRUE,
                  则使用操作系统本身的内存分配函数为Bucket分配内存,否则使用
                  PHP的内存分配函数。
  unsigned char nApplyCount; // 标记当前hash Bucket被递归访问的次数(防止多次递归)
  zend_bool bApplyProtection;// 标记当前hash桶允许不允许多次访问,不允许时,最多只能递归3次
#if ZEND_DEBUG
  int inconsistent;
#endif
} HashTable;
Copy after login

The expansion of capacity in HashTable is always adjusted to be close to the integer power of 2 of the initial size. Because:

When selecting slots, the & operation is used instead of modulo. This is because the modulo operation is relatively more expensive than the bitwise AND operation. The function of the mask is to map the hash value to the index range that the slot can store. For example: the index value of a certain key is 21, the size of the hash table is 8, then the mask is 7, and the binary representation of the sum is: 10101 & 111 = 101, which is 5 in decimal. Because the binary system of 2 to the integer power -1 is special: the values ​​of the next N bits are all 1, which makes it easier to map the values. If it is an ordinary number and is combined in binary, it will affect the result of the hash value. Then the average distribution of values ​​calculated by the hash function may be affected.

bucket:

typedef struct bucket {
  ulong h;      // 对char *key进行hash后的值,或者是用户指定的数字索引值
  uint nKeyLength;  // hash关键字的长度,如果数组索引为数字,此值为0
  void *pData;    // 指向value,一般是用户数据的副本,如果是指针数据,则指向pDataPtr
  void *pDataPtr;   //如果是指针数据,此值会指向真正的value,同时上面pData会指向此值
  struct bucket *pListNext;  // 整个hash表的下一元素
  struct bucket *pListLast;  // 整个哈希表该元素的上一个元素
  struct bucket *pNext;    // 存放在同一个hash Bucket内的下一个元素
  struct bucket *pLast;    // 同一个哈希bucket的上一个元素
// 保存当前值所对于的key字符串,这个字段只能定义在最后,实现变长结构体
  char arKey[1];       
} Bucket; 
Copy after login

The hash value is stored in the Bucket instead of the hash index.

The last field of the above structure is used to save the key string, but this field is declared as an array of only one character. In fact, this is a common variable-length structure. The main purpose is to increase flexibility. . The following is the code to apply for space when inserting new elements into the hash table

p = (Bucket *) pemalloc(sizeof(Bucket) - 1 + nKeyLength, ht->persistent);
if (!p) {
  return FAILURE;
}
memcpy(p->arKey, arKey, nKeyLength);

Copy after login

Insert process diagram

Hash algorithm

The hash function in php is implemented using the DJBX33A algorithm.

Object:

php objects are stored using the data structure zend_object_value;

Articles you may be interested in:

  • php design pattern Interpreter (interpreter mode)
  • In-depth analysis of the interpreter mode of PHP design pattern
  • PHP Kernel Exploration: Variable Storage and Type Instructions
  • PHP Kernel Exploration: Variable Overview
  • PHP Kernel Exploration: Hash Table Collision Attack Principle
  • PHP Kernel Exploration Explainer The execution process

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084571.htmlTechArticleVariables explored in the PHP kernel, php variable components: Variable name: The variable name in the php language starts with $ in English/ Underscores can include numbers, underscores, and letters, and are case-sensitive. Meanwhile...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template