PHP 8.3 Yeni Özellikler ve Geliştirmeler
zafer ak
Yazar
PHP 8.3, Kasım 2023'te yayınlandı ve birçok yeni özellik, performans iyileştirmesi ve syntax geliştirmesi içeriyor.
Typed Class Constants
Artık class constant'larına tip belirtebilirsiniz:
<?php
// PHP 8.3 öncesi
class OldClass {
public const STATUS = 'active';
}
// PHP 8.3
class NewClass {
public const string STATUS = 'active';
public const int MAX_ITEMS = 100;
public const array ALLOWED_TYPES = ['a', 'b', 'c'];
}
// Interface ile
interface Configurable {
public const string CONFIG_KEY = 'config';
}
#[Override] Attribute
Bir metodun parent class'ı override ettiğini açıkça belirtir:
<?php
class ParentClass {
public function process(): void {}
}
class ChildClass extends ParentClass {
#[Override]
public function process(): void {
// Parent metodunu override eder
}
#[Override]
public function prccess(): void {
// HATA! Parent'ta böyle bir metod yok (typo)
}
}
json_validate() Fonksiyonu
JSON string'ini parse etmeden doğrulama:
<?php
// PHP 8.3 öncesi
function isValidJson(string $json): bool {
json_decode($json);
return json_last_error() === JSON_ERROR_NONE;
}
// PHP 8.3
$json = '{"name": "John", "age": 30}';
if (json_validate($json)) {
$data = json_decode($json);
// İşlem yap
}
// Daha hızlı, bellek kullanmaz
$invalid = '{invalid json}';
var_dump(json_validate($invalid)); // false
Randomizer Geliştirmeleri
<?php
use Random\Randomizer;
$randomizer = new Randomizer();
// Yeni: getBytesFromString()
$chars = 'abcdefghijklmnopqrstuvwxyz';
$randomString = $randomizer->getBytesFromString($chars, 10);
echo $randomString; // Örn: "kxmqwpzjnt"
// Yeni: getFloat() ve nextFloat()
$float = $randomizer->getFloat(0.0, 1.0);
echo $float; // Örn: 0.7234
// Interval bounds
$float = $randomizer->getFloat(
0.0,
1.0,
\Random\IntervalBoundary::ClosedOpen
);
Dynamic Class Constant Fetch
<?php
class Config {
public const DATABASE = 'mysql';
public const CACHE = 'redis';
}
// PHP 8.3 öncesi
$name = 'DATABASE';
$value = constant(Config::class . '::' . $name);
// PHP 8.3
$name = 'DATABASE';
$value = Config::{$name};
echo $value; // 'mysql'
Readonly Amendments
<?php
// Deep cloning readonly properties
readonly class User {
public function __construct(
public string $name,
public DateTime $createdAt
) {}
public function __clone(): void {
// PHP 8.3'te readonly property'leri clone içinde değiştirebilirsiniz
$this->createdAt = clone $this->createdAt;
}
}
Diğer Yenilikler
Negative Array Index
<?php
// Boş array'e ilk eleman eklendiğinde index 0
$arr = [];
$arr[] = 'first';
var_dump($arr); // [0 => 'first']
unserialize() Error Handling
<?php
// Daha iyi hata mesajları
try {
unserialize('invalid');
} catch (ValueError $e) {
echo $e->getMessage();
}
DateTimeImmutable::createFromTimestamp()
<?php
// Yeni metod
$dt = DateTimeImmutable::createFromTimestamp(1699900800);
echo $dt->format('Y-m-d H:i:s');
Deprecations
get_class()parametresiz kullanım deprecatedget_parent_class()parametresiz kullanım deprecated- Bazı
$GLOBALSkullanımları
Sonuç
PHP 8.3, dilin güvenliğini ve tutarlılığını artıran önemli özellikler sunuyor. Yeni projelerde PHP 8.3 kullanmanızı öneriyoruz.