Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for 🚀 𝗣𝗛𝗣 𝟴.𝟰 𝗶𝘀 𝗛𝗲𝗿𝗲: 𝗗𝗶𝘀𝗰𝗼𝘃𝗲𝗿 𝗪𝗵𝗮𝘁'𝘀 𝗡𝗲𝘄 𝘄𝗶𝘁𝗵 𝗘𝘅𝗮𝗺𝗽𝗹𝗲𝘀!
Mhammed Talhaouy
Mhammed Talhaouy

Posted on • Edited on

     

🚀 𝗣𝗛𝗣 𝟴.𝟰 𝗶𝘀 𝗛𝗲𝗿𝗲: 𝗗𝗶𝘀𝗰𝗼𝘃𝗲𝗿 𝗪𝗵𝗮𝘁'𝘀 𝗡𝗲𝘄 𝘄𝗶𝘁𝗵 𝗘𝘅𝗮𝗺𝗽𝗹𝗲𝘀!

The latest release of PHP,PHP 8.4, is packed with features that simplify code, improve performance, and align with modern development practices. Here’s a quick look at the highlights, along with examples:

🔹Property Hooks

Eliminate boilerplate getters and setters! Customize property access behavior directly in your classes.

classUser{privatestring$hashedPassword;publicstring$password{get=>'***';// Masked password for displayset(string$plainPassword){$this->hashedPassword=password_hash($plainPassword,PASSWORD_DEFAULT);// Hash the password}}publicfunctionverifyPassword(string$plainPassword):bool{returnpassword_verify($plainPassword,$this->hashedPassword);}}// Example usage$user=newUser();// Set the password$user->password='secure123';// Access the password (masked)echo$user->password;// Output: ***// Verify the passwordvar_dump($user->verifyPassword('secure123'));// bool(true)var_dump($user->verifyPassword('wrongpassword'));// bool(false)
Enter fullscreen modeExit fullscreen mode

🔹Asymmetric Visibility

Control reading and writing access with different visibility levels for properties.

classBankAccount{publicfunction__construct(publicreadonlyfloat$balance=0.0){}privatefunctionsetBalance(float$amount):void{// Business logic for updating the balance}}$account=newBankAccount(100.0);echo$account->balance;// Public for reading// $account->balance = 200.0; // Error: Cannot modify (private for writing)
Enter fullscreen modeExit fullscreen mode

🔹New Array Find Functions

Work smarter with arrays using new utility functions likearray_find andarray_all.

$users=[['id'=>1,'name'=>'Alice'],['id'=>2,'name'=>'Bob'],];$user=array_find($users,fn($user)=>$user['name']==='Bob');print_r($user);// Output: ['id' => 2, 'name' => 'Bob']$isAllAdults=array_all($users,fn($user)=>$user['age']>=18);
Enter fullscreen modeExit fullscreen mode

🔹Class Instantiation Without Extra Parentheses

Chain methods or access properties seamlessly without redundant parentheses.

classTask{publicfunctionstatus():string{return'Completed';}}echo(newTask)->status();// Output: Completed
Enter fullscreen modeExit fullscreen mode

🔹HTML5 DOM Parser

The DOM parser now fully supports HTML5, improving web compatibility.

$dom=newDOMDocument();$dom->loadHTML('<!DOCTYPE html><html><body><div>Hello</div></body></html>');echo$dom->saveHTML();// Parses HTML5 content properly
Enter fullscreen modeExit fullscreen mode

🔹Deprecations

Stay ahead of breaking changes:

  • Implicit nullable types are deprecated.
  • Session usage via GET/POST has been deprecated.

💡Why It Matters:

PHP 8.4 empowers developers to write cleaner, more efficient, and modern code. Whether you're building web applications, APIs, or anything in between, these features make development more enjoyable.

👉 Which feature excites you the most? Let’s discuss in the comments!

Top comments(7)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
speratus profile image
Andrew Luchuk
Just your average coder. Hoping to learn something new and share my own knowledge along the way.
  • Location
    Virginia
  • Education
    Flatiron School
  • Work
    Full Stack Web Developer at diamondLife
  • Joined

Friendly heads up, a number of the features you mention here are not new to PHP 8.4. In particular these features have existed in PHP for a while:

  • magic__get and__set methods
  • class instantiation without parenthesis
CollapseExpand
 
tal7aouy profile image
Mhammed Talhaouy
Sr Software Engineer @Maxmind | PHP Laravel, Vue.js | Innovating with LLMs
  • Location
    None
  • Joined
CollapseExpand
 
speratus profile image
Andrew Luchuk
Just your average coder. Hoping to learn something new and share my own knowledge along the way.
  • Location
    Virginia
  • Education
    Flatiron School
  • Work
    Full Stack Web Developer at diamondLife
  • Joined

I have read the release notes. The example of property hooks in the article just uses PHP magic methods. Those have existed for a really long time. If you look at the release notes sample of property hooks, you'll see that property hooks are not magic methods.

Here is a proper example of property hooks:

classA{publicstring$demo;publicstring$demo{set(string$newDemo){$this->demo=preg_replace("/World/","property hook",$newDemo);}}}$a=newA;$a->demo="Hello, world";echo$a->demo;// Prints "Hello, property hook"
Enter fullscreen modeExit fullscreen mode

Asymmetric visibility is new, but the example of it here does not illustrate asymmetric visibility. Instead, it illustrates features that have been around for a while.

An example of asymmetric visibility:

classA{publicprivate(set)$demo='Hello World';// Outside classes can get the property, but cannot set it.}
Enter fullscreen modeExit fullscreen mode

Finally, class instantiation without parentheses is not new. I've used it for years now. If you read the release notes carefully, you will see that the class instantiation without extra parentheses is actually different.

Pre PHP 8.4:

classA{publicfunctionmethod(){}}(newA)->method();// the `new A` syntax has been available for some time. I don't know which version it was added in, but I have used it in PHP 7.4.
Enter fullscreen modeExit fullscreen mode

PHP 8.4 Version:

classA{publicfunctionmethod(){}}newA()->method();
Enter fullscreen modeExit fullscreen mode
Thread Thread
 
tal7aouy profile image
Mhammed Talhaouy
Sr Software Engineer @Maxmind | PHP Laravel, Vue.js | Innovating with LLMs
  • Location
    None
  • Joined

I already did but thank you bro :)

CollapseExpand
 
xwero profile image
david duymelinck
Learned to code in the wild west time of php 4, also the time xml and xpath where the new hot thing.
  • Location
    Belgium
  • Joined
• Edited on• Edited

For the property hooks example. I think you mix application output with internal output. I understand why you have this example, but it is too coupled with the user output for me.

The asymmetric visibility example is not correct. The error come from the readonly attribute. if you want asymmetric visibility you need something likepublic private(get) float $balance = 0.0; Although the readonly attibute also is related to visibility. It is more narrow than the asymmetric visibility that is added to php 8.4.

CollapseExpand
 
xoubaman profile image
Carlos Gándara
I do software, mainly with PHP. Also I read about it. And I think about it.
  • Location
    A Coruña, Spain
  • Work
    Dev at GotPhoto
  • Joined

As mentioned by@speratus some of the examples provided are misleading because they don't use the new 8.4 features. Would be good to fix them.

CollapseExpand
 
zeestech profile image
zees-tech
  • Joined

git@github.com:zees-tech/resumebuilder.git

check code and feel free to update

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Read next

abeertech01 profile image

TanStack Table - Quick Learning

Abdul Ahad Abeer -

oigorrudel profile image

ShedLock: Scheduling + Horizontal Scaling

Igor Rudel -

sudhanshudevelopers profile image

JavaScript Arrow Functions & Ternary Operator Explained

Sudhanshu Gaikwad -

testwithtorin profile image

TDD vs BDD: Which Testing Method Drives Better Code Quality?

Torin Vale -

Sr Software Engineer @Maxmind | PHP Laravel, Vue.js | Innovating with LLMs
  • Location
    None
  • Joined

More fromMhammed Talhaouy

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp