Editors Note, February 14th 2022: This project is now ABANDONED and no longer supported or updated.

asherwunk/phabstractic now has a None “data type”.

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, whereas 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 its 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 false, 0, 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 None Class

NOTE: Due to a naming conflict with the null keyword in PHP 7 this class has been changed to reflect a new class name of None, rather than Null.

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

namespace Phabstractic\Data\Types
{
    class None
    {
        const NULL = null;

        public $null = null;

        public function null()
        {
            return null;
        }
    }
}

As you can see, all of its associated data paraphernalia resolves to the built-in data ‘type’ null. I didn’t identify a static class member because I thought the constant None::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.

This is part of the Phabstractic Library.

photo credit: 20081011-_MG_2873.jpg via photopin (license)

Asher Wolfstein

Metaverse Resident

About the Author

A metaverse resident, you can find me on Second Life (kadar.talbot) and other online platforms. I write about my digital life, my musings, and my projects as a programmer, webmaster, artist, and game designer. (exist (be wunk) (use rational imagination) (import artist coder maker furry) (conditional (if (eq you asshole) (me (block you))))

View Articles