This file contains 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 | |
class User { | |
public int $id; | |
public string $name; | |
} |
This file contains 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 | |
$factor = 10; | |
$nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]); | |
// $nums = array(10, 20, 30, 40); |
This file contains 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 | |
class A {} | |
class B extends A {} | |
class Producer { | |
public function method(): A {} | |
} | |
class ChildProducer extends Producer { | |
public function method(): B {} | |
} |
This file contains 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 | |
$array['key'] ??= computeDefault(); | |
// is roughly equivalent to | |
if (!isset($array['key'])) { | |
$array['key'] = computeDefault(); | |
} |
This file contains 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 | |
$parts = ['apple', 'pear']; | |
$fruits = ['banana', 'orange', ...$parts, 'watermelon']; | |
// ['banana', 'orange', 'apple', 'pear', 'watermelon']; |
This file contains 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 | |
$float = 6.674_083e-11; | |
$decimal = 299_792_458; | |
$hexadecimal = 0xCAFE_F00D; | |
$binary = 0b0101_1111; |
This file contains 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 | |
mb_strtoupper("Straße"); | |
// Produces STRAßE on PHP 7.2 | |
// Produces STRASSE on PHP 7.3 |
This file contains 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 | |
mb_ereg('(?<word>\w+)', '国', $matches); | |
// => [0 => "国", 1 => "国", "word" => "国"]; |
This file contains 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(object $obj) : object | |
{ | |
return new SplQueue(); | |
} | |
test(new StdClass()); |
This file contains 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 | |
abstract class A | |
{ | |
abstract function test(string $s); | |
} | |
abstract class B extends A | |
{ | |
// overridden - still maintaining contravariance | |
// for parameters and covariance for return |
OlderNewer