Skip to content

Instantly share code, notes, and snippets.

View Chemaclass's full-sized avatar
🏗️
building

Jose M. Valera Reales Chemaclass

🏗️
building
View GitHub Profile
@Chemaclass
Chemaclass / ParameterTypeWidening.php
Last active September 21, 2019 22:41
Docu - Parameter type widening
<?php
interface A
{
public function test(array $input);
}
class B implements A
{
public function test($input){} // type omitted for $input
}
@Chemaclass
Chemaclass / AllowTrailingCommaGroupedNamespaces.php
Last active September 21, 2019 22:41
Docu - Allow a trailing comma for grouped namespaces
<?php
use Foo\Bar\{
Foo,
Bar,
Baz,
};
@Chemaclass
Chemaclass / NullableTypes.php
Created September 21, 2019 22:42
Docu - Nullable types
<?php
function test(?string $name)
{
var_dump($name);
}
test('elePHPant');
test(null);
test();
?>
@Chemaclass
Chemaclass / VoidFunctions.php
Created September 21, 2019 22:43
Docu - Void functions
<?php
function swap(&$left, &$right): void
{
if ($left === $right) {
return;
}
$tmp = $left;
$left = $right;
$right = $tmp;
@Chemaclass
Chemaclass / SymmetricArrayDestructuring.php
Created September 21, 2019 22:46
Docu - Symmetric array destructuring
<?php
$data = [
[1, 'Tom'],
[2, 'Fred'],
];
// list() style
list($id1, $name1) = $data[0];
// [] style
@Chemaclass
Chemaclass / ClassConstantVisibility.php
Created September 21, 2019 22:47
Docu - Class constant visibility
<?php
class ConstDemo
{
const PUBLIC_CONST_A = 1;
public const PUBLIC_CONST_B = 2;
protected const PROTECTED_CONST = 3;
private const PRIVATE_CONST = 4;
}
@Chemaclass
Chemaclass / IterablePseudoType.php
Created September 21, 2019 22:47
Docu - iterable pseudo-type
<?php
function iterator(iterable $iter)
{
foreach ($iter as $val) {
//
}
}
@Chemaclass
Chemaclass / MultiCatchExceptionHandling.php
Created September 21, 2019 22:48
Docu - Multi catch exception handling
<?php
try {
// some code
} catch (FirstException | SecondException $e) {
// handle first and second exceptions
}
@Chemaclass
Chemaclass / SupportForKeysInList.php
Created September 21, 2019 22:48
Docu - Support for keys in list()
<?php
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
// list() style
list("id" => $id1, "name" => $name1) = $data[0];
// [] style
@Chemaclass
Chemaclass / SupportForNegativeStringOffsets.php
Created September 21, 2019 22:49
Docu - Support for negative string offsets
<?php
var_dump("abcdef"[-2]); // string (1) "e"
var_dump(strpos("aabbcc", "b", -3)); // int(3)
/*
* Negative string and array offsets are now also supported in
* the simple variable parsing syntax inside of strings.
*/
$string = 'bar';
echo "The last char of '$string' is '$string[-1]'";