PHP code example of lisachenko / scalar-objects

1. Go to this page and download the library: Download lisachenko/scalar-objects library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lisachenko / scalar-objects example snippets


"hello"->length();                          // 5
"  hello  "->trim()->upper();               // "HELLO"
(42)->toFloat();                            // 42.0
[1, 2, 3]->map(fn (int $x): int => $x * 2); // [2, 4, 6]
[1, 2, 3, 4]->filter(fn ($x) => $x % 2 === 0)->sum(); // 6
"hello"->length(...);                       // even first-class callables work

use Lisachenko\ScalarObjects\Handler\NullHandler;
use Lisachenko\ScalarObjects\Registry;
use Lisachenko\ScalarObjects\ScalarType;

Registry::register(ScalarType::Null, NullHandler::class);

(null)->coalesce('fallback'); // "fallback"

use Lisachenko\ScalarObjects\TypeHandler;

final class ShoutHandler extends TypeHandler
{
    public function shout(): string
    {
        return strtoupper((string) $this->value) . '!';
    }
}

Registry::register(ScalarType::String, ShoutHandler::class);

"beep"->shout(); // "BEEP!"

use Lisachenko\ScalarObjects\AstRewriter;

AstRewriter::kip these even inside them
bash
php -d ffi.enable=1 -d opcache.jit=off vendor/bin/phpunit