Last active
July 6, 2017 14:34
-
-
Save ircmaxell/205bb6cb00d9a4cf8035 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function fibo(int $n): int { | |
return match($n) { | |
case 1: 1; | |
case 0: 1; | |
case _<0: throw new Exception("Less than 0 is not allowed, " . _ . " provided"); | |
case _: fibo(_ - 1) + fibo(_ - 2); | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$people = [ | |
["name" => "foo", "age" => 25], | |
new User("bar", 33), | |
["user" => new User("baz", 31)], | |
]; | |
function getAge($user) { | |
return match($user) { | |
case ["age" => _]: _; | |
case User: $user->getAge(); | |
case ["user" => _]: _->getAge(); | |
} | |
} | |
getAge($people[0]); // 25 | |
getAge($people[1]); // 33 | |
getAge($people[2]); // 31 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I was looking at your examples, which PHP version are you using? I cannot find any documentation about the match() statement, not even in the RFCs.