Home > Article > Backend Development > Use of PHP7.2 Data Structures
This article mainly introduces the use of PHP7.2 Data Structures, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
pecl install ds
brew install homebrew/php/php71-ds
Currently PHP7.2 does not support installation using brew.
Array
In the era of PHP5.x, Array
is the only data type that represents a collection in PHP , he is both List and Map, he is everything.
<?php $a = array(1,2,3,4); $b = array('a'=>1,'b'=>2,'c'=>3);
This data type does bring convenience to developers, but it allows PHPer to ignore the benefits of the data structure, especially when learning other languages. Troubled.
After PHP was upgraded to 7, Array
was also optimized, but its structure did not change, "optimized for everything; optimized for nothing" with room for improvement. So if we can optimize performance by introducing more convenient data structures, and writing code will be more convenient at the same time, then why not?
"What about SPL data structures?"
Unfortunately they are terrible. They did offer some benefits prior to PHP 7, but have since been neglected to the point of having no practical value.“Why can’t we fix and improve them?”
We could, but I believe their design and implementation is so poor that it would be better to replace them with something brand new.“The design of SPL data structures is terrible.” - Anthony Ferrara
PHP's Array can get null when accessing a non-existent key, and no fatal error will occur, but there will be an E_NOTICE. This E_NOTICE will be intercepted by the function registered by set_error_handler. Obviously, this kind of unclean code and unnecessary performance overhead can be completely avoided.
<?php $a = []; $a['a']; // PHP Notice: Undefined offset
General PHPer will not use array_key_exists and if else to handle it, which will be a bit troublesome.
Sometimes when using Array, the performance will become very poor. Array is essentially a Map. Unshifting an element will change the key of each element. This is an O(n) operation. In addition, PHP's Array saves its value (including key and its hash) in a bucket, so we need to check each bucket and update the hash.
PHP internally completes the array_unshift operation by creating a new array, and the performance issues can be imagined.
DataStructures, an extension of PHP7, a replacement for array (Array).
Github: https://github.com/php-ds
Namespace: Ds\
Interface class: Collection, Sequence, Hashable
Implementation class (final class): Vector, Deque, Map, Set, Stack, Queue, PriorityQueue, Pair
Collection is a basic interface that defines the basic operations of a data collection (the collection here refers to Collection, not Set) , such as foreach, echo, count, print_r, var_dump, serialize, json_encode, and clone. etc.
Sequence is the basic interface of array-like data structures and defines many important and convenient methods, such as contains, map, filter, reduce, find, first, last, etc. As can be seen from the figure, Vector, Deque, Stack, and Queue all directly or indirectly implement this interface. Its characteristics are as follows:
The value will always be indexed [0, 1, 2, …, size - 1]
Deletion or insertion updates the position of all consecutive values.
Only allows access to values with indexes in [0, size-1].
#Hashable looks isolated in the diagram, but is important for Maps and Sets. If an Object implements Hashable, it can be used as the key of Map and the element of Set. In this way, Map and Set can be used as conveniently as Java.
Vector should be one of the most commonly used data structures. You can think of it as Ruby's Array or Python's List. The index of its element's value is its index in the buffer, so it is very efficient. You can use it as long as you need to use an array and do not need insert, remove, shift and unshift.
Video description
The main data structure used in PhotoShop is Vector ---- Sean Parent
.
. Hashable only introduces two methods: hash and equals. The data structures that support the Hashable interface are Map and Set.
, it is a linear search with a complexity of O(n). If you want to create an array of unique values, you can use
array_unique()
. Since array_unique()
targets value rather than key, each array member will be restricted. Searching, the complexity will become O(n²). The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to compile and install extended redis and swoole in phpService container and dependency injection in PHP AnalysisThe above is the detailed content of Use of PHP7.2 Data Structures. For more information, please follow other related articles on the PHP Chinese website!