Phabstractic: Restricted Lists Data Types

asherwunk/phabstractic now has restricted list data type support.

Restricted Lists

A list is a type of abstract data type that in essence, keeps pieces of data in a certain order, like a stack or a queue.  I’ve written about Stacks, and Queues in regards to asherwunk/phabstractic.  I’ve also written about the Restrictions predicate that is available in the library.

Well, now it’s time to put those two things together and introduce the Restricted List!

Remember the ListInterface?  Our new list will implement the following as inheriting from AbstractList (github):

We add and remove elements onto the list using ->push()  and ->pop() , and we index into the list using ->index()  and ->indexReference() .  In our implementation we’re going to have more methods than this, in fact, we inherit from AbstractList (github):

For this particular list implementation it doesn’t really matter if we’re a stack or a queue, as if we want a restricted version of those two we’ll make implementations of those.  For now, we must implement the Abstract Restricted List.  This in essence serves as the middle man between implementation and restriction.  The methods implemented in this abstract class are meant to be called as parts of the final implementation’s methods.  In order to keep our list only holding values of data types we approve of, we simply filter what goes into them.  Here you can see the ->push() methods set up to call a function which passes the value by the Restrictions predicate set up in the constructor (github):

The idea here is that an implementation will call the parent::push(), acting on that return value before executing its own code.  These push methods here encapsulate the behavior we are seeking: they pass a value by the type restrictions object that accepts or rejects it based on what it is (a scalar, a number, an object, a certain class, etc. see the restrictions predicate for more information).  Then, depending on the return value of the parent method, it will act on that data to either reject or add it to the list.  RestrictedList.php has been implemented to give us an example (in this case happening to implement a basic array) (github):

Note that as in the previous list types we are sure to specify the ArrayAccess methods.  But note their difference in the Restricted List.  They must also check against data type restrictions, and we haven’t forgotten that:


An example of using this functionality is say I wanted a list of automobiles, but only automobiles.  They are forming a queue and are going to be allowed through a gate.  It happens that my Car class is in the namespace MyNamespace\Automobile\Car.  I would first instantiate my restrictions to match that data type, and then instantiate a list:

Then when I am ready to put a car in the list I might do something like this:

Note, that if we try to add a value to the list that is not proper, since we’ve set it strict, it will throw an error:


Lists are very important pieces of software engineering and implementation.  We have studied lists such as stacks and queues before, now we have simply implemented them with type checking.  PHP is not a typed language, any variable can be anything, but with a restrictions predicate we are able to ensure variables in PHP are specific types.  If we combine these two things we get a restricted list.

This is part of the Phabstractic Library.

If you appreciate my programming please help support me through my Patreon.

photo credit: Promenade, Torquay via photopin (license)


I'm just a wunk, trying to enjoy life. I am a cofounder of http// and I like computers, code, creativity, and friends.

You may also like...

Leave a Reply

%d bloggers like this: