PHP 8.3 contains many new features, such as explicit typing of class constants, deep-cloning of readonly properties and additions to the randomness functionality.
New Features 8.3
// Typed class constants // Include class, trait, interface, enum traitTestTrait{ finalprotectedconststring TEST = 'test'; } interfaceTestInterface{ publicconststring TEST = 'test'; } enumTestEnum: string{ publicconststring TEST = 'test'; }
// Similar to return types, class constant types can also be "narrowed", or kept the same in a sub-class or an implementation. This follows LSP. classParentClass{ publicconststring|int VALUE = 'MyValue'; } classChildClassextendsParentClass{ publicconststring VALUE = 'MyValue'; }
// Dynamic class constant and Enum member fetch support classFoo{ constPHP = 'PHP 8.3'; } $searchableConstant = 'PHP'; echoconstant(Foo::class . "::{$searchableConstant}"); echoFoo::{$searchableConstant}; // instead of `constant()`
// Random extension // public function getFloat( // float $min, float $max, // \Random\IntervalBoundary $boundary = \Random\IntervalBoundary::ClosedOpen // ): float // // enum IntervalBoundary { // case ClosedOpen; // case ClosedClosed; // case OpenClosed; // case OpenOpen; // } $rng = newRandom\Randomizer(); echo$rng->getFloat(0, 10, \Random\IntervalBoundary::OpenOpen); // 6.7810757668383
// Generate a random number in the range 0 <= and < 1: $rng = newRandom\Randomizer(); echo$rng->nextFloat(); // 0.21185336351144
// Returns a random number sequence of a requested length ($length parameter), only containing a bytes from the requested series of bytes ($string parameter). $rng = newRandom\Randomizer(); $chars = 'AEIOU'; echo$rng->getBytesFromString($chars, 1); // "E" echo$rng->getBytesFromString($chars, 5); // "AIIEO"
// #[\Override] attribute // PHP enforces that the targeted class method must be overriding or implementing a parent/interface method. classParentClass{ protectedfunctiondemo() {} } classChildClassextendsParentClass{ #[\Override] protectedfunctiondemo() {} // ✅ 明确覆盖
// Pad a multibyte string to a certain length with another multibyte string echomb_str_pad('中国', 6, '爱', STR_PAD_RIGHT) . "\n"; echomb_str_pad('中国', 6, '爱', STR_PAD_LEFT) . "\n"; echomb_str_pad('中国人', 6, '爱', STR_PAD_BOTH) . "\n"; // 中国爱爱爱爱 // 爱爱爱爱中国 // 爱中国人爱爱
Backward Incompatible Changes 8.3
// Assigning a negative index n to an empty array will now make sure that the next index is n + 1 instead of 0. $arr = []; $arr[-5] = 'a'; $arr[] = 'b'; var_dump($arr); // PHP8.3 → [-5 => 'a', -4 => 'b'] // PHP <8.3 → [-5 => 'a', 0 => 'b']