PHP code example of dgame / php-optional

1. Go to this page and download the library: Download dgame/php-optional 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/ */

    

dgame / php-optional example snippets


$some = some(42);
$this->assertTrue($some->isSome());
$this->assertEquals(42, $some->unwrap());

$some = some(42);
$this->assertTrue($some->isSome($value));
$this->assertFalse($some->isNone());
$this->assertEquals(42, $value);

$none = none();
$this->assertTrue($none->isNone());
$this->assertFalse($none->isSome());

$none = none();
$this->assertTrue($none->isNone());
$this->assertFalse($none->isSome($value));
$this->assertNull($value);

$maybe = maybe(null);
$this->assertTrue($maybe->isNone());
$maybe = maybe(42);
$this->assertTrue($maybe->isSome());
$this->assertEquals(42, $maybe->unwrap());

$result = some(0)->ensure(function($value) {
    return $value > 0;
});
$this->assertTrue($result->isNone());