PHP 8.1 — yeniliklere hızlı bakış

image source: php.net

Kurulum

$ brew update
$ brew tap shivammathur/php
$ brew install php@8.1
$ brew link --overwrite --force php@8.1
$ php -vPHP 8.1.0 (cli) (built: Nov 24 2021 20:21:42) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.0, Copyright (c) Zend Technologies
with Zend OPcache v8.1.0, Copyright (c), by Zend Technologies

Enums

<?phpclass TaskStatus
{
public const CREATED = 1;
public const IN_PROGRESS = 2;
public const COMPLETED = 3;
public const FAILED = 4;
}
<?phpclass Task
{
// ...
// class properties and methods
private int $status; public function updateStatus(int $status)
{
$this->status = $status;
}
public function status(): int
{
return $this->status;
}
}
<?php$task = new Task;$task->updateStatus(TaskStatus::CREATED);echo $task->status(); // 1
<?phpenum TaskStatus
{
case CREATED;
case IN_PROGRESS;
case COMPLETED;
case FAILED;
}
<?phpclass Task
{
// ...
// class properties and methods
private TaskStatus $status; public function updateStatus(TaskStatus $status)
{
$this->status = $status;
}
public function status(): TaskStatus
{
return $this->status;
}
}
<?php$task = new Task;$task->updateStatus(TaskStatus::CREATED);echo $task->status(); // Error
echo $task->status()->name; // name of the Enum// ORecho $task->status()->value; // value of the Enum
enum TaskStatus: int
{
case CREATED = 10;
case IN_PROGRESS = 20;
case COMPLETED = 30;
case FAILED = 40;
}
echo TaskStatus::FAILED->value; // 40
echo TaskStatus::FAILED->name; // FAILED
enum TaskStatus: int
{
case CREATED = 10;
case IN_PROGRESS = 20;
case COMPLETED = 30;
case FAILED = 40;
public function message(): string
{
return match ($this) {
case self::CREATED => 'Task Created',
case self::IN_PROGRESS => 'Task In Progress',
case self::COMPLETED => 'Task Completed',
case self::FAILED => 'Task Failed',
};
}
}
<?php$task = new Task;$task->updateStatus(TaskStatus::CREATED);echo $task->status()->message(); // Task Created

Read-Only Properties

<?phpclass Task
{
// public readonly int $age;
public function __construct(
public readonly string $name,
public readonly string $email,
public readonly string $city
) {
}
}
<?php$task = new Task('John Doe', 'john@doe.com', 'Ankara');echo $task->name; // John Doe
echo $task->email; // john@doe.com
$task->email = 'johndoe@doe.com'; // Error. Cannot change the value

Intersection Types

<?phpfunction square(int|float $number): int|float
{
return $number ** 2;
}
<?phpfunction example(Foo&Bar $param): void
{
// function block
}
<?phpclass ExampleClass implements Foo, Bar
{
// class block
}

Never Return Type

<?phpfunction example(): never
{
// function block

die;
// or exit;
}

array_is_list function

<?php$list    = ['a', 'b', 'c'];
$notList = [1 => 'a', 'b', 'c'];
$list_2 = [0 => 'a', 'b', 'c'];
var_dump(array_is_list($list)); // true
var_dump(array_is_list($notList)); // false
var_dump(array_is_list($list_2)); // true

First class callable syntax

<?phpfunction sum(float ...$numbers): float
{
return array_sum($numbers);
}
$closure = Closure::fromCallable('sum');echo $closure(2, 3, 5); // 10
<?phpfunction sum(float ...$numbers): float
{
return array_sum($numbers);
}
$closure = sum(...);echo $closure(2, 3, 5); // 10

‘new’ keyword in initializer

<?phpclass ExampleClass
{
public function __construct(public ?Foo $foo = null)
{
$this->foo ??= new Foo();
}
}
<?phpclass ExampleClass
{
public function __construct(public ?Foo $foo = new Foo())
{}
}
<?php$example = new Example(new Foo()); // It's okay$example = new Example(); // It's okay

‘final’ Class Constants

<?phpclass Foo
{
public const NUMBER = 10;
}
class Bar extends Foo
{
public const NUMBER = 20;
}
echo Bar::NUMBER; // 20
<?phpclass Foo
{
final public const NUMBER = 10;
}
class Bar extends Foo
{
public const NUMBER = 20; // This will not be valid
}

Array unpacking support for string-keyed array

<?php$managers  = ['John', 'Jane'];
$employees = ['Michael', 'Sara', 'Alex', 'Lincoln'];
// old
// $team = array_merge($managers, $employees);
// with unpacking
$team = [...$managers, ...$employees];
// $managers = ['Burak', ...$managers];
<?php$arrayA = ['a' => 1];
$arrayB = ['b' => 2];
$result = ['a' => 0, ...$arrayA, ...$arrayB];// ['a' => 1, 'b' => 2]

Fiber — Async PHP, Concurrency

Ve dahası…

PHP Foundation Kuruldu

Son Söz

Şimdilik bu kadar.

--

--

--

nam-ı diğer Buki. 👨🏻‍💻 . Software Engineer — #php #java #golang #javascript #react-native— Co-Founder at HaberSistemim & MemleketApp

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
İzni Burak Demirtaş

İzni Burak Demirtaş

nam-ı diğer Buki. 👨🏻‍💻 . Software Engineer — #php #java #golang #javascript #react-native— Co-Founder at HaberSistemim & MemleketApp

More from Medium

Laravel model presenters with the Reflection API

Reflection

GraphQL API using PHP and Laravel

Deep dive into Laravel Pagination — PHP IteratorAggregate

How To Use Single Responsibility Principle In PHP/Laravel With Example