A rundown of the changes in PHP8
|
Class/interface name
|
An instance of the class or interface
|
|
self
|
The same class the type is used in
|
|
parent
|
The parent of the class the type is used in
|
|
callable
|
Something callable
|
|
bool
|
A boolean
|
|
int
|
An integer
|
|
float
|
A float
|
|
string
|
A string
|
|
array
|
An array
|
|
iterable
|
Array or traversable
|
|
object
|
An object
|
|
mixed
|
Any value
|
|
Return Types
|
|
|
void
|
Function will not return anything
|
|
static
|
Function may return
static |
mixed type
|
Mixed means any one of:
|
|||
|
array
|
bool
|
callable
|
|
|
string
|
int
|
resource
|
|
|
float
|
null
|
object
|
|
mixed already contains null, it is not nullable itself.
class Foo
{
public function test(): static
{
return new static();
}
}
$foo = $bar->getBaz()?->asFormatted();
public function foo(Foo|Bar $input): int|float;
void type cannot be part of a union.null as a type or preceding with a question mark.
function foo(string $a, int $b, ?string $c = null)
{
...
}
foo(
c: 'value c',
a: 'value a'
);
use App\Attributes\ExampleAttribute;
#[ExampleAttribute]
class Foo
{
#[ExampleAttribute]
public const FOO = 'foo';
#[ExampleAttribute]
public $x;
#[ExampleAttribute]
public function foo(#[ExampleAttribute] $bar) { }
}
$result = match($input) {
0 => "hello",
'1', '2', '3' => "world",
};
match use strict types.
class Money
{
public function __construct(
public Currency $currency,
public int $amount,
) {}
}
Currency and int are both automatically made public properties of the class and assigned values on construction.
class Foo
{
private WeakMap $cache;
public function getSomethingWithCaching(object $obj): object
{
return $this->cache[$obj]
??= $this->computeSomethingExpensive($obj);
}
}
$triggerError = fn () => throw new MyError();
$foo = $bar['offset'] ?? throw new CustomEx('offset');
try {
// Something goes wrong
} catch (MySpecialException) {
Log::error("Something went wrong");
}
$foo = new Foo();
var_dump($foo::class);
get_class().
public function(
string $parameterA,
int $parameterB,
Foo $objectfoo,
) {
// …
}
DateTime::createFromInterface(DateTimeInterface $other);
DateTimeImmutable::createFromInterface(DateTimeInterface $other);
class Foo
{
public function __toString(): string
{
return 'foo';
}
}
function bar(string|Stringable $stringable) { /* … */ }
bar(new Foo());
bar('abc');
str_contains('haystack', 'needle') |
Does haystack contain needle?
|
str_starts_with('haystack', 'needle') |
Does haystack start with needle?
|
str_ends_with('haystack', 'needle') |
Does haystack end with needle?
|
fdiv() |
Handles division by zero without an error.
|
get_debug_type() |
Like
gettype() but with more detail |
get_resource_id() |
Fetch the numeric ID of a resource
|
|
Internal errors and engine warnings have been made consistent and/or reclassified.
|
|
The
@ operator no longer silences fatal errors. |
|
The default error reporting level is now
E_ALL. |
|
The default PDO error mode is no longer silent.
|
|
Concatenation precedence has changed - addition now precedes concatenation.
|
|
Namespaces can now include reserved names.
|
ReflectionFunction::isDisabled(), ReflectionParameter::getClass() and ReflectionParameter::isCallable() have been deprecated. |
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.