Java like safe types in PHP
|
ASMCC++JAVAPHP BINARYSOURCE ANDROIDLINUXMACWINDOWS |
Description
Set of classes that wrap base types like int, float, bool. Useful for type hinting on PHP5+, and as guarantee that value has correct type. This will help limit usage of is_bool, is_int etc.
Classes constructors and set member is throwing InvalidArgumentException if invalid type is assigned, for example: $b = new Bool( "boo" ) will throw exception, for more examples please take a look at usage.
TODO:
- Make String immutable like in Java, and add most used str functions
- Add set of valueOf
- Add toHexString, ToBinString to all number types
Usage
1. Type hint in function/class member
function typeTest( Bool $val ) {}
2. Type guarantee
$b = new Bool( true ); //ok
$b->set( false ); //ok
$b->set( "true" ); //Exception string is not bool
$b->set( null ); //Exception null is not correct value
$b->set( false ); //ok
$b->set( "true" ); //Exception string is not bool
$b->set( null ); //Exception null is not correct value
3. Function parameters guarantee
function typeTest( Bool $val ) {}
$b = new Bool( true );
$f = new Float( 1.0 );
typeTest( $b ); // ok $b has correect type
typeTest( $f ); // Catchable fatal error – incorrect type
$b = new Bool( true );
$f = new Float( 1.0 );
typeTest( $b ); // ok $b has correect type
typeTest( $f ); // Catchable fatal error – incorrect type