Get ready to explore the fantastic world of PHP 8.3, where coding gets a cool upgrade! In this super-detailed guide, we’ll take you through impressive PHP 8.3’s features, performance boosters, and what the friendly coding community is buzzing about. Whether you’re a coding pro or just starting, let’s discover the secrets of PHP 8.3 together.
Table of Contents
About PHP 8.3 Update
PHP 8.3 is the latest upgrade in the coding world which was released on 23 November 2023. It adds cool new features, makes things run faster, and makes sure your code is more secure. One exciting thing it does is use a Just-In-Time (JIT) compiler to speed up how your code works. It also brings in something called enumerations to make your code design better. Whether you’re a pro coder or just starting out, PHP 8.3 has got something for everyone, making coding more fun and exciting!
PHP 8.3 Features and Improvements
1. Typed Class Constants
We’ve had the ability to declare types for class properties since PHP 7.4. Despite various modifications in PHP over the years, it hasn’t yet been applied to constants until now.
In PHP 8.3, class constants, including interface, trait, and enum constants can be typed.
For Example:
class Test { const string TEST_CONSTANT = 'test'; }
2. The json_validate()
function
Initially, the only way to figure out whether a string was valid JSON was to decode it and detect the errors if there were any. However, the new json_validate()
function takes less memory than decoding the string and can be helpful if you simply need to know whether the input is valid JSON or not.
For Example:
json_validate(string $json, int $depth = 512, int $flags = 0): bool
3. Tweaks in readonly
PHP 8.1 introduced the option to specify individual class properties as readonly
. The ability to apply the attribute to an entire class was introduced in PHP 8.2. However, many developers felt that the limits caused while working with classes that contained such characteristics limited useful programming.
Two proposals were proposed in an RFC for modifying readonly
behavior:
- Allow non
readonly
classes to extendreadonly
classes. - When cloning, allow
readonly
properties to be initialized.
This second proposal has been accepted into PHP 8.3. The new approach enables the reinitialization of instances of a class with readonly
attributes within the __clone
magic method.
For Example:
readonly class Student { public function __construct( public DateTime $createdAt, ) {} public function __clone() { $this->createdAt = new DateTime(); // This is allowed, // even though `createdAt` is a readonly property. } }
4. New #[Override]
Attribute
The programmer’s intent is displayed via the new #[Override]
attribute. In simple terms, it says, “I understand that this way is overriding a parent method. Please inform me if that were to change at any point“.
For Example:
class A { protected function overrideTest(): void {} } // This will work because ovrTest() // can be found in the parent class class B extends A { #[\Override] public function overrideTest(): void {} } // This will fail because overrideBest() // (probably a typo) is not in the parent class C extends A { #[\Override] public function overrideBest(): void {} }
5. Dynamic Class Constant Fetch
Fetching class constants with variable names has proven to be a little more complex than with other properties in PHP code. You may have done it using the constant()
function in a manner similar to this before PHP 8.3.
For Example:
class Person { const NAME = 'John'; } $name = 'NAME'; // Instead of this: constant(Person::class . '::' . $name); // You can now do this: Person::{$name};
5. New getBytesFromString()
Method
This new PHP 8.3 feature helps to create random strings with a pre-selected set of characters. It’s getBytesFromString()
method, which was included in the Random extension, makes it simple to accomplish now.
This new approach is straightforward, you give it a string of characters to work with and tell it how many of them to utilize. After that, until the string reaches the desired length, the function will randomly select bytes from it.
For Example:
$rando = new Random\Randomizer(); $alpha = 'ABCDEFGHJKMNPQRSTVWXYZ'; $rando->getBytesFromString($alpha, 8); // "MBXGWLAV" $rando->getBytesFromString($alpha, 8); // "LESPMGWK" $rando->getBytesFromString($alpha, 8); // "NVHWXCPU"
It’s possible that the random output’s specified length will contain more bytes than the input string:
$rando = new Random\Randomizer(); $nums = '12345'; $rando->getBytesFromString($nums, 8); // "43121523"
6. New getFloat()
and nextFloat()
Methods
The next feature includes two new methods, getFloat()
and nextFloat()
, that generate random float values, further building on the Random extension.
Following the minimum and maximum values, a third parameter is also accepted by the getFloat()
method.
If the Enum is not specified as the third option when calling getFloat()
, IntervalBoundary::ClosedOpen is the default.
Example on getFloat()
:
$rando = new Random\Randomizer(); printf( "Lat: %+.6f Long: %+.6f", $rando->getFloat(-90, 90, \Random\IntervalBoundary::ClosedClosed), // -180 will not be used $rando->getFloat(-180, 180, \Random\IntervalBoundary::OpenClosed), );
the new nextFloat()
function is practically the same as using getFloat()
when requesting a random value that falls between 0 and less than 1.
Example on nextFloat()
:
$rando = new Random\Randomizer(); $rando->nextFloat(); // 0.4679027143784
Additional Minor Changes in PHP 8.3 Features List
Several further new features and small adjustments are also included in this PHP version. We’ll include these below along with links for further resources.
- Additional methods for the DOMElement class:
DOMElement::getAttributeNames()
,DOMElement::insertAdjacentElement()
,DOMElement::insertAdjacentText()
,DOMElement::toggleAttribute()
,DOMNode::contains()
,DOMNode::getRootNode()
,DOMNode::isEqualNode()
- New methods for the IntlCalendar class:
IntlCalendar::setDate()
,IntlCalendar::setDateTime()
,IntlGregorianCalendar::createFromDate()
,IntlGregorianCalendar::createFromDateTime()
- New LDAP functions:
ldap_connect_wallet()
andldap_exop_sync()
. - New
mb_str_pad()
multibyte string function. - New POSIX functions:
posix_sysconf()
,posix_pathconf()
,posix_fpathconf()
andposix_eaccess()
. - New
ReflectionMethod::createFromMethodName()
method. - New socket function:
socket_atmark()
. - New string functions:
str_increment()
,str_decrement()
,stream_context_set_options()
. - New ZipArchive class method:
ZipArchive::getArchiveFlag()
. - New INI setting to set the maximum allowed stack size:
zend.max_allowed_stack_size
.
PHP 8.3 Deprecations
Some PHP functions and settings are marked for removal with each new release. These features are no longer supported for use and generate warnings in numerous logs when they appear in executing code.
- The U_MULTIPLE_DECIMAL_SEPARATORS constant is deprecated in favor of U_MULTIPLE_DECIMAL_SEPARATORS.
- When a negative number n is assigned to an empty array, It will now make sure that the next index is
n + 1
instead of0
. - Changes to the
range()
function. - SQLite3: Default error mode set to exceptions.
- The
3MT_RAND_PHP
Mt19937 variant is deprecated. ReflectionClass::getStaticProperties()
is no longer nullable.- More Appropriate Date/Time Exceptions.
- INI settings
assert.active
,assert.bail
,assert.callback
,assert.exception
, andassert.warning
are deprecated. - Changes in re-declaration of static properties in traits.
- Calling
get_class()
andget_parent_class()
without arguments have been deprecated.
Conclusion
Simply put, PHP 8.3 transforms web development, making it faster and easier for developers. With improved performance and simpler coding, PHP ensures efficient debugging and seamless integration with modern tools. Real-world examples prove its impact, making it a go-to for better project performance. Staying updated with every version of PHP is crucial for mastering the ever-changing web development landscape. It’s not just the latest; PHP 8.3 is a game-changer, shaping the future of coding.
Ever wondered about spicing up your WordPress blog without the hassle of plugins? Discover our exclusive guide on “Display Reading Time on a WordPress Blog Without a Plugin” It’s a simple trick to elevate your blog’s user experience.
FAQs: Answering Your Curious Questions
Q.1 Why is PHP 8.3 a big deal?
It brings speed, clarity in code, and enhanced security, making it a game-changer for developers.
Q.2 Is upgrading to PHP 8.3 from an older version complicated?
Not at all! It is designed to smoothly transition from older versions, making the upgrade process friendly for developers.
Q.3 Are there any backward compatibility issues when migrating to PHP 8.3?
While it brings new features, it’s crucial to review the official PHP migration guide to identify any potential backward compatibility issues. Testing your code in a controlled environment before the upgrade is recommended to ensure a smooth transition.
Q.4 What security features does PHP 8.3 bring?
It introduces new security features to protect your code from common vulnerabilities, ensuring a more robust coding environment.
Q.5 Can I use attributes on class constants in PHP 8.3?
Yes, it allows the use of attributes on class constants. This feature enhances metadata handling in your code and provides more flexibility in documenting and organizing your classes.