PHP Data Structures (Java-like) Collections

I am interested in learning about the benefits of creating a small library, possibly for personal use, which defines several data structures, such as Linked Lists, Trees (Binary, AVL, etc.), Hash lookup tables, etc.

Some of them will be built on top of their own PHP array, since it acts like many of these types, and some potentially will not.

My question is, does it make sense to build any of these classes - especially those that cannot be based on their own array? I am talking about computational practicality here and don’t particularly want to go into the dynamic or typed language argument (I’m still interested to hear something interesting and relevant in this topic aside).

Is it crazy to build these (possibly more efficient, logical) data structures using classes when we have a C implementation of the base array?

Thanks..

+4
source share
4 answers

There is already http://php.net/manual/en/book.spl.php which may cover what you want, but if their creation will be interesting for you and improve your PHP / general then I think it will be very good idea.

It can also be a good library to release a wider audience and something that could be added to your resume.

+1
source

Look at the SPL

http://php.net/manual/en/book.spl.php

and read Matthew Turland New Spliple Features in PHP 5.3 for a discussion of these data structures.

+2
source

This is a late answer, but it will help someone look for PHP data structures. PHP 7 introduces an extension called ds that provides specialized data structures as an alternative to an array.

ds

  • uses the Ds\ namespace.
  • has 3 interfaces, namely Collection , Sequence and Hashable .
  • It has 8 classes, namely: Vector , Deque , Queue , PriorityQueue , Map , Set , Stack and Pair .

For more information, check out Manual as well as This blog post contains some awesome information, including benchmarks.

+1
source

If you are looking at performance, you can create a php module (e.g. SPL) that provides access to these structures (since they are already implemented in C).

0
source

Source: https://habr.com/ru/post/1343002/


All Articles