Primus2\Falcraft: New Data Types (null, range)


This code has been deprecated and moved.  It is now part of it’s own package and project on github called phabstractic (a pure PHP library of abstract programming concepts).  There is more current information on the Null data type, Range data type, and phabstractic.

Many programming languages, including PHP, have a built in type (or two) for dealing with ’empty’ objects.  There usually is a distinction between a value (variable) that has been declared and not (in PHP this is the difference between a variable being defined, and unset() ).  On top of that though, there is usually a ‘null’ value, that being a variable that is storing ‘nothing’.  It’s not really appropriate to say ‘nothing’, as that’s not really how I picture it at least.  It’s basically a value that is not a value, it’s not a number, not a string, just null.

Note that this is not the same as false, though null may resolve as a false ‘truth-value’.  In programming, a variable that is solely false is a Boolean, where-as a null is just a blank-out.  This is very similar to how 0 and false are different values.  strpos() in PHP may return 0 for the beginning of a string, or false when the string doesn’t contain the key.  Both of these resolve to false ‘truth-values’, but they are definitely different values with different meanings.

Often a function returns null as an error, to show that the subroutine didn’t complete it’s task, or that the function could not compute a value.  This allows for return types to include Boolean, Integers, etc as part of their return type, so that 0 can be a legitimate return value.

But what happens when you want to differentiate between false0, and null?  Or in another context, what if you need a null value that is considered an object by the algorithm or language?

The solution is to define a custom class/object structure that you can identify as your ‘null object’.\

The Null Class

I have implemented a custom class/object structure that can resolve as true under object instanceof Null , as well as have member variables and functions also resolve to the built-in null data value.  It’s basic source follows (also on github):

As you can see, all of its associated data paraphernalia resolves to he built-in data ‘type’ null.  I didn’t identify a static class member because I thought the constant Null::NULL fulfilled that position satisfactorily.

This allows a function to return the built-in null as an appropriate value in its calculations as well as false or zero.  On top of that, for algorithms that must operate on an object, or expect an object (such as a loop using the instanceof operator) this object substitutes or short-circuits in a null value without much fanfare.

The Range Class

When you use an if statement you often times poll whether a given value is equal to, or lesser/greater than a given value.  When I published my PHP Restrictions Predicate object (Primus2/Falcraft) I was interested in being able to abstract away the nature of an if clause.  I thought, what if the idea of something coming out as true or false was encapsulated in an object as a ‘first class citizen’?  This was interesting to me, because I could then set up if-clauses that checked for certain parameters (filters as I called them in my post) and then use them as swappable and configurable components in an algorithm’s logic.

Think about it in terms of a sorting function.  A sorting function takes two items given to it by an internal sorting function and returns which one is first and which one is second.  You can plug a sorting function into a sorting algorithm and produce custom list-processing functionality purely based on that sorting function.  I transformed the notion of a ‘sorting function’ into an abstract one of a ‘filter’.  Plug any filter into anywhere that an algorithm may have a variable if-clause comparing pre-determine data types.  Heck, you could build a multi-data type predicate function that you could plug into a generic algorithm which would then allow you to add additional data-types without having to rewrite the algorithm.  If-clause polymorphism baby!

But I digress.  There was a particular set of if-clause functionality that I hoped to reproduce in a more succinct and less abstract sense, and that was inequalities.  In mathematics you get equality statements with less-than, greater-than, equal-to, greater-than-or-equal-to, etc.

What if you could define a range of values, say, the domain of a function perhaps, and then test if a value was in that range?  This could prove useful for pre-emptive debugging of function bounds.

Thus, I bring to you the Range class (also on github):

Yes, it does use the dreaded eval function, so be very cautious what kind of information you send it. However, I thought it was more elegant to use the built in comparison functions, and I think it’s pretty secure given that the only thing it references outside of itself is min and max.

It was either this or write all the comparison logic on top of comparison logic, which I thought was kind of ugly.

Now, you can test if a number is in a given range by simply calling $myRange->isInRange($number, array('maximum'=>true,))  [This example includes the maximum (less than and equal to maximum)]


It is useful to be able to differentiate in an object oriented sense if a value is null.  Sometimes you want to be able to return o, false, or null, but still need a way of indicating that there was an error.  A custom null class/object can fulfill that role for you.

Predicate objects are an interesting concept, which could have many uses in functional programming, in mathematical polymorphism, in input validation, etc.  Ranges are a specific version of a predicate object that tests if any given value is within the maximum and minimum range defined in the object.

photo credit: Old Trailer in HDR 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: