Skip to content

Instantly share code, notes, and snippets.

View ThijsFeryn's full-sized avatar

Thijs Feryn ThijsFeryn

View GitHub Profile
@ThijsFeryn
ThijsFeryn / 1_scalar_type.php
Created November 30, 2015 14:09
PHP 7 scalar type hints
<?php
/**
* Scalar type declarations
*/
//declare(strict_types=1);
function add(int $a, int $b) {
return $a + $b;
}
@ThijsFeryn
ThijsFeryn / 2_return_type.php
Created November 30, 2015 14:14
PHP 7 return type declarations
<?php
/**
* Return type declarations
*/
//declare(strict_types=1);
function add(int $a, int $b): int{
return (string)($a + $b);
}
@ThijsFeryn
ThijsFeryn / 3_anonymous_classes.php
Created November 30, 2015 14:14
PHP 7 anonymous classes
<?php
/**
* Anonymous classes
*/
$foo = new class {
public function foo() {
return "bar";
}
};
@ThijsFeryn
ThijsFeryn / 4_closure_call.php
Created November 30, 2015 14:15
PHP 7 Closure::call()
<?php
/**
* Closure::call()
*/
class Foo
{
private $foo = 'bar';
}
@ThijsFeryn
ThijsFeryn / 5_generator_delegation.php
Created November 30, 2015 14:16
PHP 7 generator delegation
<?php
/**
* Generator delegation
*/
function gen()
{
yield 1;
yield 2;
yield from gen2();
@ThijsFeryn
ThijsFeryn / 6_generator_return.php
Created November 30, 2015 14:16
PHP 7 generator return
<?php
/**
* Generator return expressions
*/
$gen = (function() {
yield 1;
yield 2;
return 3;
@ThijsFeryn
ThijsFeryn / 7_null_coalesce.php
Created November 30, 2015 14:17
PHP 7 null coalesce operator
<?php
/**
* Null coalesce operator
*/
$array = ['foo'=>'bar'];
//PHP5 style
$message = isset($array['foo']) ? $array['foo'] : 'not set';
echo $message.PHP_EOL;
@ThijsFeryn
ThijsFeryn / 8_space_ship.php
Created November 30, 2015 14:17
PHP 7 space ship operator
<?php
/**
* Space ship operator
*/
$array = [
"1 <=> 1" => 1 <=> 1,
"1 <=> 2" =>1 <=> 2,
"2 <=> 1" => 2 <=> 1
];
@ThijsFeryn
ThijsFeryn / 9_throwable.php
Created November 30, 2015 14:17
PHP 7 throwables
<?php
/**
* Throwable interface
*/
//Error as Throwable
try {
sqdf();
} catch (Throwable $t) {
@ThijsFeryn
ThijsFeryn / 10_dirname.php
Created November 30, 2015 14:18
PHP 7 dirname levels
<?php
/**
* Dirname levels
*/
echo dirname('/usr/local/bin').PHP_EOL;
echo dirname('/usr/local/bin',1).PHP_EOL;
echo dirname('/usr/local/bin',2).PHP_EOL;
echo dirname('/usr/local/bin',3).PHP_EOL;