php5.4-300x300I am a huge fan of type hinting in PHP. When using great tools like PHPStorm it gets even better because of the amazing auto-complete and refactoring capabilities the IDE provides. Unfortunately, PHP doesn’t provide type hinting for scalar values. The SPL provides a set of classes for scalars, but they are not very popular. So, the package PHP Scalars (https://github.com/joefallon/PhpScalars) was born.

Here is an example of using PHPScalars:

This provides us with two great benefits. First, the value contained within the instance of Float is guaranteed to be a floating point number. Second, the type hinting built into PHP ensures that the $radius is not null. Mitigating the proliferation of nulls is an important part of modern software development. In fact, it has often been called the Billion Dollar Mistake.

In addition to the scalars, two classes are provided for handling really large integers and floating point numbers containing any number of decimal places. This functionality is provided using the PHP BC Math library. This is especially useful for handling values representing money.

PHPScalars is fully documented, tested, and lives on GitHub. It is also a Packagist package. Feel free to check it out and provide feedback.