Phabstractic: Type Enumeration

asherwunk/phabstractic now has type detection and comparison using an enumerator generated by Phabstractic/Data/Types/Enumeration.php

Type Detection and Generation Through Enumeration

Although PHP is making strides in strong typing (See SitePoint, and HHVM) and the sparse but useable string and array keywords in multi-order functions, it doesn’t have a very approachable typing system.  This is usually the case with an interpreted language like PHP, for instance Python and PERL have very similar fluidity in its variable assignment operations.

I wanted to make it easier to determine a variables type (string, integer, array, resource) in one fell function call rather than rely on a bunch of calls to is_array(), is_int() say in an if clause.  I also wanted it to handle objects, both standard and custom, as well as the null value, functions and closures.  So, I used Data/Types/Enumeration.php to create an enumeration of all the data types (BASIC_XXX).  I instantiate this in the Type.php file itself where I also define the two functions inside another namespace.

The namespaces of the Type.php file are a little different.  I define the Enumeration encapsulated in one namespace (note, in version 3 of enumeration I don’t specify a key value pair):

This creates an enumeration class in the Phabstractic\Data\Types namespace, giving it a fully qualified identifier of Phabstractic\Data\Types\Type.  But then I create two ‘global’ functions (I decided not to use an object/class because Phabstractic/Data/Types/Type was thus taken.  Since I cannot/shouldn’t put custom functionality in the enumeration object, I thought in terms of convenience this worked well.  So the following is set:

These functions are in the same file as the enum generation.  So as soon as Data\Types\Type is loaded (the enumeration object) as a side effect these two functions are also defined. I know this isn’t PSR compatible (I don’t think), but in terms of functionality for the rest of the library it’s a very convenient side-effect.  Auto-loading should work the same though, and I haven’t run into any problems with it in previous programming incarnations.  These functions make use of the enumeration object defined before, for example:

As you can see when we poll a variable for its data type, we receive a data type back.  In the case of a custom defined object, we receive a blanket TYPED_OBJECT in an array along with the original variable for convenience.

NOTE:  These functions use the ability for an enumeration to be instantiated, and returns enum OBJECTS holding the value of their respective types.  Thus, you would access an enum instance value: ->get();  or typecast as (string)  Remember the ability to compare two objects as stated in the header of Enumeration.php.

With this data ‘type’ we would, for instance, be able to perform our own type check.  However, our type check would be entirely dynamic, as opposed to a static typing system where, in short, the variables can’t change their type once they are initialized.

View on GitHub

This is part of the Phabstractic Library.

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

photo credit: Historic Tour 2016 – Charade 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: