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 / TypedProperties.php
Last active September 21, 2019 22:22
docu - Typed properties
<?php
class User {
public int $id;
public string $name;
}
@Chemaclass
Chemaclass / ArrowFunctions.php
Created September 21, 2019 22:24
Docu - Arrow functions
<?php
$factor = 10;
$nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]);
// $nums = array(10, 20, 30, 40);
@Chemaclass
Chemaclass / LimitedReturnTypeCovarianceAndArgumentTypeContravariance.php
Created September 21, 2019 22:27
Docu - Limited return type covariance and argument type contravariance
<?php
class A {}
class B extends A {}
class Producer {
public function method(): A {}
}
class ChildProducer extends Producer {
public function method(): B {}
}
@Chemaclass
Chemaclass / NullCoalescingAssignmentOperator.php
Created September 21, 2019 22:30
Docu - Null coalescing assignment operator
<?php
$array['key'] ??= computeDefault();
// is roughly equivalent to
if (!isset($array['key'])) {
$array['key'] = computeDefault();
}
@Chemaclass
Chemaclass / UnpackingInsideArrays.php
Created September 21, 2019 22:33
Docu - Unpacking inside arrays
<?php
$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];
@Chemaclass
Chemaclass / NumericLiteralSeparator.php
Last active September 21, 2019 22:34
Docu - Numeric literal separator
<?php
$float = 6.674_083e-11;
$decimal = 299_792_458;
$hexadecimal = 0xCAFE_F00D;
$binary = 0b0101_1111;
@Chemaclass
Chemaclass / MultibyteStringFunctions.php
Created September 21, 2019 22:36
Docu - Multibyte String Functions
<?php
mb_strtoupper("Straße");
// Produces STRAßE on PHP 7.2
// Produces STRASSE on PHP 7.3
@Chemaclass
Chemaclass / NamedCapturesSupport.php
Created September 21, 2019 22:36
Docu - Named Captures Support
<?php
mb_ereg('(?<word>\w+)', '国', $matches);
// => [0 => "国", 1 => "国", "word" => "国"];
@Chemaclass
Chemaclass / NewObjectType.php
Created September 21, 2019 22:38
Docu - New object type
<?php
function test(object $obj) : object
{
return new SplQueue();
}
test(new StdClass());
@Chemaclass
Chemaclass / AbstractMethodOverriding.php
Last active September 21, 2019 22:41
Docu - Abstract method overriding
<?php
abstract class A
{
abstract function test(string $s);
}
abstract class B extends A
{
// overridden - still maintaining contravariance
// for parameters and covariance for return