Last active
August 7, 2016 22:21
-
-
Save yoander/2f90b1472fecf713cdae1a3012b5d959 to your computer and use it in GitHub Desktop.
New features of PHP 7
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 | |
define('PLAYERS', [ | |
'1' => ['Garry Kasparov', 2851], | |
'2' => ['Anatoly Karpov', 2780], | |
'3' => ['Magnus Carlsen', 2882], | |
'4' => ['Bobby Fischer', 2785] | |
]); | |
interface PlayerDetailsInterface | |
{ | |
public function getName(); | |
public function getRanking(); | |
} | |
/** | |
* Get Chess player detail | |
* | |
* @return PlayerDetailsInterface | |
*/ | |
function getChessPlayerDetails($id): PlayerDetailsInterface | |
{ | |
// Do some processing, consult some internet DB, Webservice or another resource | |
$player = PLAYERS[$id]; | |
$name = $player[0]; | |
$ranking = $player[1]; | |
return new class($name, $ranking) implements PlayerDetailsInterface | |
{ | |
private $name; | |
private $ranking; | |
public function __construct($name, $ranking) | |
{ | |
$this->name = $name; | |
$this->ranking = $ranking; | |
} | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function getRanking() | |
{ | |
return $this->ranking; | |
} | |
}; | |
} | |
foreach (array_keys(PLAYERS) as $id) { | |
/**@var PlayerDetailsInterface $detail */ | |
$detail = getChessPlayerDetails($id); | |
echo $detail->getName(), ': ', $detail->getRanking(), PHP_EOL; | |
} | |
Garry Kasparov: 2851 | |
Anatoly Karpov: 2780 | |
Magnus Carlsen: 2882 | |
Bobby Fischer: 2785 | |
*/ |
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 | |
define('DAYS', [ | |
'sun' => 'Sunday', | |
'mon' => 'Monday', | |
'tue' => 'Tuesday', | |
'wed' => 'Wednesday', | |
'thu' => 'Thursday', | |
'fri' => 'Friday', | |
'sat' => 'Saturday' | |
]); | |
// PrSunday | |
echo DAYS['sun'], PHP_EOL; |
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 gen() | |
{ | |
yield 1; | |
yield 2; | |
yield from gen2(); | |
} | |
function gen2() | |
{ | |
yield 3; | |
yield 4; | |
} | |
foreach (gen() as $val) | |
{ | |
echo $val, PHP_EOL; | |
} | |
// 1 | |
// 2 | |
// 3 | |
// 4 |
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 | |
$gen = (function() { | |
yield 1; | |
yield 2; | |
return 3; | |
})(); | |
foreach ($gen as $val) { | |
echo $val, PHP_EOL; | |
} | |
echo $gen->getReturn(), PHP_EOL; | |
// 1 | |
// 2 | |
// 3 |
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 | |
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody'; |
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 | |
// Fetches the value of $_GET['user'] and returns 'nobody' if it does not exist. | |
$username = $_GET['user'] ?? 'nobody'; | |
// This is equivalent to: | |
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; |
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
#!/bin/bash | |
# Print yes | |
perl -e '$a = $b || "yes"; print($a);' | |
# Print yes | |
php -r '$a = $b ?? "yes"; print_r($a);' | |
# Print yes | |
perl -e '$a = 0 || "yes"; print($a);' | |
# Print 0 | |
php -r '$a = 0 ?? "yes"; print_r($a);' | |
# Print I have a value | |
perl -e '$a = "I have a value" || "yes"; print($a);' | |
# Print I have a value | |
php -r '$a = "I have a value" ?? "yes"; print_r($a);' |
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 | |
declare(strict_types = 1); | |
function test_param(int $a, bool $is_ready, string $str) { | |
echo $a, ' => ', gettype($a), "\n"; | |
echo $is_ready, ' => ', gettype($is_ready), "\n"; | |
echo $str, ' => ', gettype($str), "\n"; | |
} | |
class Test { | |
public function __toString() { | |
return Test::class; | |
} | |
} | |
test_param(5, true, new Test()); | |
// Fatal error: Uncaught TypeError: Argument 3 passed to test_param() | |
// must be of the type string, object given... |
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 test_param(int $a, bool $is_ready, string $str) { | |
echo $a, ' => ', gettype($a), "\n"; | |
echo $is_ready, ' => ', gettype($is_ready), "\n"; | |
echo $str, ' => ', gettype($str), "\n"; | |
} | |
class Test { | |
public function __toString() { | |
return Test::class; | |
} | |
} | |
test_param(5.5, true, new Test()); | |
// 5 => integer | |
// 1 => boolean | |
// Test => string |
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 sum($a, $b): int { | |
return $a + $b; | |
} | |
function sum_f($a, $b): float { | |
return $a + $b; | |
} | |
class Test { | |
public function __toString() { | |
return __CLASS__; | |
} | |
} | |
function return_str(): string { | |
return (new Test()); | |
} | |
// Print 5 | |
echo sum(3.5, 2), PHP_EOL; | |
// Print 5.5 | |
echo sum_f(3.5, 2), PHP_EOL; | |
// Print Test | |
echo return_str(), PHP_EOL; |
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 | |
declare(strict_types=1); | |
function sum($a, $b): int { | |
return $a + $b; | |
} | |
function sum_f($a, $b): float { | |
return $a + $b; | |
} | |
class Test { | |
public function __toString() { | |
return __CLASS__; | |
} | |
} | |
function return_str(): string { | |
return (new Test()); | |
} | |
// Fatal error: Uncaught TypeError: Return value of sum() must be of the type | |
// integer, float returned... | |
echo sum(3.5, 2), PHP_EOL; | |
// Print 5.5 | |
echo sum_f(3.5, 2), PHP_EOL; | |
// Fatal error: Uncaught TypeError: Return value of return_str() must be of the | |
// type string, object returned... | |
echo return_str(), PHP_EOL; |
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 | |
echo 1 <=> 1; // 0 | |
echo 1 <=> 2; // -1 | |
echo 2 <=> 1; // 1 | |
echo 1.5 <=> 1.5; // 0 | |
echo 1.5 <=> 2.5; // -1 | |
echo 2.5 <=> 1.5; // 1 | |
echo "a" <=> "a"; // 0 | |
echo "a" <=> "b"; // -1 | |
echo "b" <=> "a"; // 1 |
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 | |
namespace Config { | |
const DB_DRIVER = 'mysql'; | |
const DB_HOST = 'localhost'; | |
const DB_PORT = '3306'; | |
const DB_USER = 'root'; | |
} | |
// Global namespace | |
namespace { | |
// Before/Antes PHP 7 | |
// use const Config\DB_DRIVER; | |
// use const Config\DB_HOST; | |
// use const Config\DB_PORT; | |
// use const Config\DB_USER; | |
// PHP 7 | |
use const Config\{DB_DRIVER, DB_HOST, DB_PORT, DB_USER}; | |
echo 'Connection details', PHP_EOL; | |
echo 'Driver: ', DB_DRIVER, ', Host: ', DB_HOST, ', Port: ', DB_PORT, ', user: ', DB_USER, PHP_EOL; | |
} |
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 | |
namespace Wordpress { | |
function wp_is_writable( $path ) { | |
if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) ) | |
return win_is_writable( $path ); | |
else | |
return @is_writable( $path ); | |
} | |
function bool_from_yn( $yn ) { | |
return ( strtolower( $yn ) == 'y' ); | |
} | |
} | |
// Global namespace | |
namespace { | |
// Before/Antes PHP 7 | |
// use function Wordpress\wp_is_writable; | |
// use function Wordpress\bool_from_yn; | |
// PHP 7 | |
use function Wordpress\{wp_is_writable, bool_from_yn}; | |
if (wp_is_writable(__DIR__)) { | |
echo __DIR__, ' is writeable', PHP_EOL; | |
} | |
echo bool_from_yn('y'), PHP_EOL; | |
} |
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 | |
// Before/Antes PHP 7 | |
// use Symfony\Component\Form\Extension\Core\Type\TextType; | |
// use Symfony\Component\Form\Extension\Core\Type\DateType; | |
// use Symfony\Component\Form\Extension\Core\Type\SubmitType; | |
// PHP 7 | |
use Symfony\Component\Form\Extension\Core\Type\{TextType,DateType,SubmitType}; | |
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 | |
$is_cli = false !== strpos(PHP_SAPI, 'cli'); | |
$nl = $is_cli ? PHP_EOL : nl2br(PHP_EOL); | |
if ($is_cli) { | |
echo "\u{2665}", $nl; | |
echo "\u{267B}", $nl; | |
echo "\u{260E}", $nl; | |
} else { | |
$style = '<a href="." style="font-size: 32px; color: %s; text-decoration: none;">%s</span>' . $nl; | |
echo sprintf($style, 'green', "\u{2665}"); | |
echo sprintf($style, 'red', "\u{267B}"); | |
echo sprintf($style, 'blue', "\u{260E}"); | |
echo '<button type="button">', "\u{267B}", '</button>'; | |
} | |
/** | |
* | |
* ♥ | |
* ♻ | |
* ☎ | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment