ducks-project/spl-types

Polyfill Module for SplType PHP extension. This extension aims at helping people making PHP a stronger typed language and can be a good alternative to scalar type hinting. It provides different typehandling classes as such as integer, float, bool, enum and string

v1.2.0 2020-11-25 11:28 UTC

This package is auto-updated.

Last update: 2024-04-21 10:18:40 UTC


README

Build Status

Coverage Status

Scrutinizer Code Quality Code Coverage Build Status Code Intelligence Status

License Latest Stable Version

Total Downloads Monthly Downloads Daily Downloads

Description

This extension aims at helping people making PHP a stronger typed language and can be a good alternative to scalar type hinting. It provides different typehandling classes as such as integer, float, bool, enum and string

It provides classes unavailable if you can't install SPL Types extension:

Known Issues & Limitations

SplTypes

Because limitations of PHP it is impossible to directly reaffect Spl variables like the original extension. For example:

// With Spl Extension.
$int = new \SplInt();
$int = 'test'; // Exception.

// With Spl Plyfill.
$int = new \SplInt();
$int = 'test'; // Just unset Object and affect 'test' to $int variable.

In the same way, Spl_Types polyfill is not really strict typing the extension. So, the code below is not "correct".

// With Spl Extension.
$test = 10;
$value = new \SplInt($test);
if ($test == $value) {
    echo 'OK';
}

// With Spl Plyfill.
$test = 10;
$string = new \SplInt($test);
if ($test === $string) { // Cast Error.
    echo 'OK';
}

SplInt & Splfloat

Because of PHP behaviors you can't easily do operations like:

$int = new \SplInt();
$int++; // Exception.

$int + 10; // Exception.

Unfortunately, you need to do like below:

$int = new \SplInt();

$result = (int) (string) $int + 10; // Shame...

SplEnum

As it was said, you need to manually cast your object to string in order to make comparison.

class Month extends \SplEnum {
    const __default = self::January;
    const January = 1;
    // ...
}
$enum = new Month();

// WARNING : Object of class Month could not be converted to int...
if (Month::January == $enum) {
    // KO ...
}

// But,
if (Month::January == (string) $enum) {
    // ... OK
}

SplBool

Like it was explain above, the \SplBool object isn't a strict boolean so take care about your equality test.

$bool = new \SplBool(false);
if ($bool) {
    echo 'This is true'; // Object is not null so it pass test...
}

License

This library is released under the MIT license.