Primus2\Falcraft: Type Enumeration

IMPORTANT

This code is no obsolete and does not reflect the new version, which has also moved to asherwunk/phabstractic.  You can see the current blog post.

Primus2/Falcraft now has type detection and comparison using an enumerator generated by Falcraft/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/Enum.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:

This creates an enumeration class in the Falcraft\Data\Types namespace, giving it a fully qualified identifier of Falcraft\Data\Types\Type.  But then I create two ‘global’ functions (I decided not to use an object/class because Falcraft/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.  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: ::getValue(); 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

photo credit: Strangers via photopin (license)

Liked it? Take a second to support kadar on Patreon!

kadar

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

You may also like...

Leave a Reply

%d bloggers like this: