January 24, 2024 • For devs
PHP 8.3: Whats new
We're Streply, a tool that tracks errors and manages logs quickly. It's easy to set up and compatible with many popular frameworks. Made by developers, for developers! If you need to keep an eye on errors and follow logs in your app, create free account.
PHP 8.3 is the latest release of the popular programming language that powers a significant portion of the web. With each new version, PHP continues to evolve and introduce exciting features and improvements. In this article, we will explore the key changes and enhancements brought by PHP 8.3.
Typed class constants
interface I {
const string PHP = 'PHP 8.3';
}
class Foo implements I {
const string PHP = [];
}
// Fatal error: Cannot use array as value for class constant
// Foo::PHP of type string
Dynamic class constant fetch
class Foo {
const PHP = 'PHP 8.3';
}
$searchableConstant = 'PHP';
var_dump(Foo::{$searchableConstant});
New #[\Override] attribute
By using the #[\Override] attribute, PHP will check if a method with the same name exists in a parent class or implemented interface. Adding this attribute makes it clear that overriding a parent method is intentional and helps simplify refactoring, as removing an overridden parent method will be detected.
use PHPUnit\Framework\TestCase;
final class MyTest extends TestCase {
protected $logFile;
protected function setUp(): void {
$this->logFile = fopen('/tmp/logfile', 'w');
}
#[\Override]
protected function taerDown(): void {
fclose($this->logFile);
unlink('/tmp/logfile');
}
}
// Fatal error: MyTest::taerDown() has #[\Override] attribute,
// but no matching parent method exists
Deep-cloning of readonly properties
The readonly properties can now be changed once in the magic __clone method to allow for deep-cloning of readonly properties.
class PHP {
public string $version = '8.2';
}
readonly class Foo {
public function __construct(
public PHP $php
) {}
public function __clone(): void {
$this->php = clone $this->php;
}
}
$instance = new Foo(new PHP());
$cloned = clone $instance;
$cloned->php->version = '8.3';
New json_validate() function
The json_validate() function checks if a string is valid JSON and is faster than json_decode().
var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true
New Randomizer::getBytesFromString() method
// A \Random\Engine may be passed for seeding,
// the default is the secure engine.
$randomizer = new \Random\Randomizer();
$randomDomain = sprintf(
"%s.example.com",
$randomizer->getBytesFromString(
'abcdefghijklmnopqrstuvwxyz0123456789',
16,
),
);
echo $randomDomain;
New Randomizer::getFloat() and Randomizer::nextFloat() methods
$randomizer = new \Random\Randomizer();
< br>
$temperature = $randomizer->getFloat(
-89.2,
56.7,
\Random\IntervalBoundary::ClosedClosed,
);
$chanceForTrue = 0.1;
// Randomizer::nextFloat() is equivalent to
// Randomizer::getFloat(0, 1, \Random\IntervalBoundary::ClosedOpen).
// The upper bound, i.e. 1, will not be returned.
$myBoolean = $randomizer->nextFloat() < $chanceForTrue;
Command line linter supports multiple files
The command line linter now takes multiple filenames as input to check for linting. Pretty cool, right?
php -l foo.php bar.php
No syntax errors detected in foo.php
No syntax errors detected in bar.php
What else
- New DOMElement::getAttributeNames(), DOMElement::insertAdjacentElement(), DOMElement::insertAdjacentText(), DOMElement::toggleAttribute(), DOMNode::contains(), DOMNode::getRootNode(), DOMNode::isEqualNode(), DOMNameSpaceNode::contains(), and DOMParentNode::replaceChildren() methods.
- New IntlCalendar::setDate(), IntlCalendar::setDateTime(), IntlGregorianCalendar::createFromDate(), and IntlGregorianCalendar::createFromDateTime() methods.
- New ldap_connect_wallet(), and ldap_exop_sync() functions.
- New mb_str_pad() function.
- New posix_sysconf(), posix_pathconf(), posix_fpathconf(), and posix_eaccess() functions.
- New ReflectionMethod::createFromMethodName() method.
- New socket_atmark() function.
- New str_increment(), str_decrement(), and stream_context_set_options() functions.
- New ZipArchive::getArchiveFlag() method.
- Support for generation EC keys with custom EC parameters in OpenSSL extension.
- New INI setting zend.max_allowed_stack_size to set the maximum allowed stack size.
- php.ini now supports fallback/default value syntax.
- Anonymous classes can now be readonly.
- We are not pushy
- We only send a few emails every month. That's all.
- No spam
- We only send articles, and helpful tips for developers, not SPAM.