Phabstractic: Structure Data Type

asherwunk/phabstractic now supports the Structure abstract data type, this is like a C structure, but with some added functionality.

Can PHP Be Like C?

Obviously, the answer is no, there’s no way PHP could handle all the lower level functionality that C and C++ allow you to address.  But there is a data structure found in C, and other programming languages, that can be somewhat emulated in PHP… if not actually done better.

This is a structure.  A structure is a bit like the TaggedUnion, only that it doesn’t necessarily change data format.  Instead, it requires particular data types grouped together.  Think of it like the fore-bearer of classes.  It’s a bunch of data stuck together without methods.  In C this is handy because all the data happens one right after another, and it can be accessed as its own data type.  In PHP, with OOP, and other things this isn’t as big of a deal.

However, we can do some interesting things with a Structure data type.  First, we can use the Restrictions Predicate to constrain some elements (fields) of the Structure to particular data types (including classes and interfaces).  This is as simple as building in some functionality to have fields of the Structure represented as Tagged Unions.  For more information on the Tagged Union abstract data type, check out this post.

Two Different Constructors

There is one bit of trouble with the constructor of the Structure, though.

The first version of the constructor (version 2) was written somewhat backwardsly in that you specified field names in values of an array.  These were processed for TaggedUnions and such, and then ‘flipped’ so that the values became keys, and then merged with the TaggedUnions (which can’t flip into a key).  Upon review, this wasn’t the best implementation, but it seemed straight forward at the time.

The new version of the constructor (version 3) is written so that an array is passed with the keys as the field names and the values as their initial state, or in the case of TaggedUnions, a TaggedUnion object or Restrictions predicate.  The code to process this and the resulting input format is much more superior to what I was doing before.  I’m not sure what I was thinking before, I guess I thought it important that the fields be represented by an indiced array.

Here’s the constructor, you can see both versions.  You can access the old version of doing things by specifying ‘version2’ in the options array passed to the constructor:

Thus the following version 2 input array:

Is equivalent to the following version 3 input array:

I think that’s a lot easier to understand, don’t you?

Can We Normalize?

Another thing version 2 of the Structure abstract data type would do was ‘denormalize’ the input to the field name of the structure, regardless if the structure was defined as being case-insensitive (see below).  In version 3 I eliminated this, and field names must now be referenced with case-sensitivity (even after specifying case-insensitive in the constructor, see below).  Here you can see the denormalize method, and how it’s only called in version 2:

When Does Case-Insensitive Not Mean Case-Insensitive?

I thought it was important in version 3 to make the field names of a structure be case-sensitive.  I thought this would eliminate errors in the long run, and allow for more flexibility.  As you can see above, version 2 doesn’t really follow this thought and denormalizes all incoming field names.  So what’s the point of the case-insensitive option?

The case-insensitive option in the constructor (‘insensitive’) is for the constructor only.  That means when the field names are defined in the structure they are defined in all lower case.  From then on, they must be referred to in case-sensitive fashion.  So, you must always use lower case when identifying them outside of the class with version 3.

What Can This Do?

Well, the Structure abstract data type does something kind of neat: it implements arrayAccess methods:

This means that in code we can actually set and interact with TaggedUnion members as if they are just ‘plain’ array elements.  The following is valid for both a non-restricted and restricted data field in a structure:


Structures are a data structure supported by many different languages.  They are not as prevalent in modern interpreted languages, which generally favor more object-oriented programming approaches.  The usefulness I think of this particular abstract data type is the ability to be accessed as an array, enabling TaggedUnions to act like normal variables.


(View on github)

This is part of the Phabstractic Library.

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

photo credit: Forest in the Desert 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: