Skip to content

Instantly share code, notes, and snippets.

@antonydandrea
Last active December 1, 2016 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonydandrea/5558e0c97ce949daa78a7c8b3fee5770 to your computer and use it in GitHub Desktop.
Save antonydandrea/5558e0c97ce949daa78a7c8b3fee5770 to your computer and use it in GitHub Desktop.
A showcase of some new features in PHP 7.1. Can read about it at antonydandrea.com/new-features-in-php-71. The file itself probably won't run, you can just copy and paste these functions individually to experiment.
<?php
/**
* Demo of Nullable Types. The ?string states that
* the function can return a string or Null.
*/
function getItem($id) : ?string
{
$animals = array("cat", "dog", "hamster", "rabbit");
if (isset($animals[$id])) {
return $animals[$id];
} else {
return null;
}
}
var_dump(getItem(5));
/**
* Indicating the nullable type being used as a parameter.
*/
function getNumberOfChars(?string $string)
{
$numberOfChars = 0;
if (!is_null($string)) {
$numberOfChars = strlen($string);
}
return $numberofChars;
}
/**
* Demo of the void type.
*/
function doSomethingRandom() : void
{
rand(0,10) * rand(0,10);
}
doSomethingRandom();
/**
* Pre 7.1 use off the list function.
*/
$animals = array("cat", "dog", "hamster", "rabbit");
list($animal1, $animal2, $animal3, $animal4) = $animals;
print $animal1;
print $animal2;
print $animal3;
print $animal4;
/**
* From 7.1 you can use keys and so deconstruct associative arrays.
*/
$animals = array("feline" => "cat", "canine" => "dog", "cricetine" => "hamster", "leporine" => "rabbit");
list("cricetine" => $animal1, "feline" => $animal2, "leporine" => $animal3, "canine" => $animal4) = $animals;
print $animal1;
print $animal2;
print $animal3;
print $animal4;
/**
* Numeric keys can also be used so you could deconstruct in any order.
*/
$animals = array("cat", "dog", "hamster", "rabbit");
list(2 => $animal1, 0 => $animal2, 3 => $animal3, 1 => $animal4) = $animals;
/**
* From 7.1, you can use this shorthand usage of list() with just [].
*/
$animals = array("cat", "dog", "hamster", "rabbit");
[2 => $animal1, 0 => $animal2, 3 => $animal3, 1 => $animal4] = $animals;
print $animal1."\n";
print $animal2."\n";
print $animal3."\n";
print $animal4."\n";
/**
* Demo of catching multiple Exceptions.
*/
class CustomException extends Exception{}
class AnotherException extends Exception{}
/**
* Throws one of the two exceptions declared above.
*/
function throwException()
{
$i = rand(0,1);
if ($i === 0) {
throw new CustomException;
} else {
throw new AnotherException;
}
}
/**
* This is how you would have to do it < PHP 7.1, and still
* can if you wish.
*/
try {
throwException();
} catch (CustomException $e) {
print "there was an error\n";
} catch (AnotherException $e) {
print "there was an error\n";
}
/**
* PHP 7.1 way of handling multiple exceptions.
*/
try {
throwException();
} catch (CustomException | AnotherException $e) {
print "there was an error\n";
}
*/
/**
* Demonstrating the ability of changing the visibility of
* class constants. The var_dump would cause an error as the constant
* is private.
*/
class MyClass
{
private const SOME_CONSTANT = 10;
}
$class = new MyClass;
var_dump($class::SOME_CONSTANT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment