2017 © Pedro Peláez
 

package php-optional

Handle PHP null values gracefully

image

stiiv/php-optional

Handle PHP null values gracefully

  • Tuesday, May 29, 2018
  • by stiiv
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

php-optional

Handle PHP null values gracefully, (*1)

Example:

source from (http://nitschinger.at/A-Journey-on-Avoiding-Nulls-in-PHP/), (*2)

<?php

include "vendor/autoload.php";

use Optional\Optional;

$possible = Optional::of(5);
var_dump($possible->isPresent()); // bool(true)
var_dump($possible->get()); // int(5)
var_dump($possible->getOrElse(99)); // int(5)
var_dump($possible->getOrNull()); // int(5)


$possible = Optional::of(null); // Throws 'NullPointerException' with message 'Unallowed null in reference found.'
$possible = Optional::fromNullable(null);
var_dump($possible->isPresent()); // bool(false)
var_dump($possible->get()); // Throws IllegalStateException
var_dump($possible->getOrElse(99)); // int(99)
var_dump($possible->getOrNull()); // NULL

$val1 = Optional::fromNullable(5);
$val2 = Optional::fromNullable(4);
$val3 = Optional::fromNullable(4);

var_dump($val1->equals($val2)); // bool(false)
var_dump($val2->equals($val3)); // bool(true)

The Versions

29/05 2018

dev-master

9999999-dev

Handle PHP null values gracefully

  Sources   Download

MIT

by Ivan Stipić Stiiv

php php optional null php handle null