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 / GeneratorDelegation.php
Last active September 23, 2019 12:08
Docu - Generator delegation
<?php
function gen()
{
yield 1;
yield 2;
yield from gen2();
}
function gen2()
{
@Chemaclass
Chemaclass / GeneratorReturnExpressions.php
Created September 21, 2019 22:56
Docu - Generator Return Expressions
<?php
$gen = (function() {
yield 1;
yield 2;
return 3;
})();
foreach ($gen as $val) {
echo $val, ' ';
}
@Chemaclass
Chemaclass / GroupUseDeclarations.php
Created September 21, 2019 22:56
Docu - Group use declarations
<?php
// Pre PHP 7 code
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;
use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;
@Chemaclass
Chemaclass / ClosureCall.php
Last active September 21, 2019 22:55
Docu - Closure::call
<?php
class A {
private $x = "1";
}
// Pre PHP 7 code
$getX = function() {return $this->x;};
$getXCB = $getX->bindTo(new A, 'A'); // intermediate closure
echo $getXCB(); // outputs "1"
@Chemaclass
Chemaclass / UnicodeCodepointEscapeSyntax.php
Created September 21, 2019 22:54
Docu - Unicode codepoint escape syntax
<?php
echo "\u{aa}"; // ª
echo "\u{0000aa}"; // ª (the same but with optional leading 0's)
echo "\u{9999}"; // 香
@Chemaclass
Chemaclass / AnonymousClasses.php
Created September 21, 2019 22:54
Docu - Anonymous classes
<?php
interface Logger {
public function log(string $msg);
}
class Application {
private $logger;
public function getLogger(): Logger {
return $this->logger;
@Chemaclass
Chemaclass / ConstantArraysUsingDefine.php
Created September 21, 2019 22:52
Docu - Constant arrays using `define()`
<?php
define('ANIMALS', [
'dog',
'cat',
'bird'
]);
echo ANIMALS[1]; // outputs "cat"
@Chemaclass
Chemaclass / SpaceshipOperator.php
Created September 21, 2019 22:52
DOcu - Spaceship operator
<?php
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
@Chemaclass
Chemaclass / NullCoalescingOperator.php
Created September 21, 2019 22:51
Docu - Null coalescing operator
<?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';
// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
@Chemaclass
Chemaclass / ReturnTypeDeclarations.php
Created September 21, 2019 22:51
Docu - Return type declarations
<?php
function arraysSum(array ...$arrays): array
{
return array_map(function(array $array): int {
return array_sum($array);
}, $arrays);
}
print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));
?>