Download the PHP package fleshgrinder/value without Composer
On this page you can find all versions of the php package fleshgrinder/value. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fleshgrinder/value
More information about fleshgrinder/value
Files in fleshgrinder/value
Package value
Short Description A modern replacement for PHP’s gettype function.
License Unlicense
Informations about the package value
Value
Modern replacement for PHP’s gettype
function.
- Installation
- Usage
Installation
Open a terminal, enter your project directory and execute the following command to add this package to your dependencies:
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Usage
The Value
class currently contains a single static method called getValue
and constants with the names of each of PHP’s types, corresponding to the
values returned by the getValue
method. There are currently no plans to
extend this API and the following functionality is considered feature complete
unless new types are added or old ones are removed.
The above will output the following:
This output corresponds to the available constants in the class with the exception for the concrete class names and the additionally available callable and iterable pseudo type names:
The method comes in very handy during the creation of error messages. One of the most repeated patterns in the PHP world is the following:
Which is not only cumbersome but also produces inconsistent type names due to
PHP’s inconsistent gettype
function. This can be replaced with the method
provided by this library.
The method never throws anything and does not emit errors. The sole goal of this tiny library is to keep code DRY as much as possible, after all, it is one of the most important principle of them all.
I tried to get this change into PHP core but it was rejected, see
PHP RFC: var_type
and the linked
resources at the bottom for more details.