DEV Community

Cover image for How to write a different PHP?

How to write a different PHP?

Adnan Babakan (he/him) on May 10, 2020

Hey there DEV.to community. PHP is one of the most discussed programming languages out in the development world. Some people call it a dead progra...
Collapse
 
erhankilic profile image
Erhan Kılıç • Edited

I never use arrow function. If I wanted to use javascript, I would use it not php. Arrow function destoys the readabilty of the code.
I don't like using everything if it's new, you need to think about it if it's worth or not.

Collapse
 
adnanbabakan profile image
Adnan Babakan (he/him)

Hi Erhan
PHP and JavaScript are different and I don't think only just because you want to use arrow functions you would switch to JavaScript.
On the opposite hand, I think arrow functions increase the readability.

Collapse
 
erhankilic profile image
Erhan Kılıç

Well, If you work on your own project alone it can be ok but real world isn't like that. You can work with a team on a big project and it can be really really messy with arrow function.
I know it because I worked on a big javascript project with a big team and arrow function makes it really mess in my opinion.
Right now my php team doesn't allow using arrow function and I'm happy about it.

Thread Thread
 
adnanbabakan profile image
Adnan Babakan (he/him)

I respect your opinion but really disagree about being messy since arrow functions are made to make code more clear if your function is not that much complicated. If you wanted to use a global variable in PHP you should GLOBAL or $_GLOBAL which is not needed in arrow functions.

Collapse
 
patabah profile image
PatAbah

For functions that perform simple operations, I'll rather a bunch of arrow functions instead.
IMO, It makes such functions more readable 😁

Collapse
 
rehmatfalcon profile image
Kushal Niroula

It would have been much more useful if the arrow syntax could be used for multiline function.

I actually like that it closes over the value of the enclosing scope. It would have been much more useful when creating an anonymous function that uses data from the enclosing scope.

<?php
function create($name,$age) {
    $this->em->transactional(fn() {
        $person = new Person($name,$age);
        $this->em->persist($person);
        $this->em->flush();
    });
}
Collapse
 
adnanbabakan profile image
Adnan Babakan (he/him) • Edited

Yes, it would have been awesome. Maybe in the later version, they will add such a feature.

Collapse
 
jinoantony profile image
Jino Antony

It would be helpful if you can add the PHP version from which each feature is available. I think most features you mentioned only available in PHP 7.4

Collapse
 
dwilmer profile image
Daan Wilmer

New in 7.4 are the arrow functions and the $foo ??= 'bar; assignment. 7.2 brought class-based type hinting and return types, and everything else was included in 7.0 - including $too = $foo ?? 'bar'; and scalar type hinting.

Collapse
 
easrng profile image
easrng

I love that a

✨Spaceship operator 🚀

exists.

Collapse
 
biros profile image
Boris Jamot ✊ /

Nice post, thanks!

Collapse
 
davidfrafael profile image
David Fernández

Doesn't Laravel automatically trim the values of the request?

Collapse
 
adnanbabakan profile image
Adnan Babakan (he/him)

For some reason sometimes there is an inconsistency. That's why I added another layer or triming. LOL

Collapse
 
zahedomid profile image
OmidZahed

so cool

Collapse
 
chuckych profile image
Norberto CH

Muy Bueno... Gracias.

Collapse
 
adnanbabakan profile image
Adnan Babakan (he/him)

Gracias para leer mi articulo. xD

Collapse
 
karimboudjema profile image
Karim Boudjema

Don't forget you can also assign values in the assignment part:
$type = ($age >= 18) ? 'adult' : 'no adult';
is the same as
($age >= 18) ? $type = 'adult' : $type = 'no adult';

Collapse
 
detzam profile image
webstuff • Edited

The single question that i have, cuz i m lazy at the moment, is this a 7.x feature? Or is it possible on 5.6.x , too?

Collapse
 
adnanbabakan profile image
Adnan Babakan (he/him)

Hi
I believe they are 7.x features.

Collapse
 
inxomnyaa profile image
Dan

$base_url = $url ? $url : 'localhost';

shouldn't that be

$base_url === $url ? $url : 'localhost';

otherwise the condition will always be true because you assign a variable..

Collapse
 
adnanbabakan profile image
Adnan Babakan (he/him)

Hi Dan
Not it is not a condition, it is an assignment statement.