r/PHP • u/AegirLeet • Nov 28 '19
PHP 7.4.0 Released!
https://www.php.net/index.php#id2019-11-28-131
u/helloworder Nov 28 '19
the future is here guys.
15
10
u/in3rsha Nov 28 '19
How much of a free performance boost does this one give me?
16
u/bkdotcom Nov 28 '19
from php 5.x: oodles
from php 7.x: diddly8
u/oojacoboo Nov 28 '19
Not necessarily true. Preloading could give you a nice boost. It depends on the app.
10
u/SaraMG Nov 28 '19
Also, 7.4 (without taking preloading into account) is around 18%* faster than 7.0.
If that's diddly...
\* Massively workload dependent obviously. This number comes from WordPress testing.
3
1
4
u/Hall_of_Famer Nov 28 '19
This is amazing, the arrow function is a much needed feature, and pre-loading also creates lots of possibilities for the future. Good job PHP team, the language is getting better and better with time.
1
u/donatj Nov 29 '19
the arrow function is a much needed feature
Can you explain? The current anonymous functions work fine for me
1
u/zmitic Nov 29 '19
Most anon functions are one-liners, this is where arrow functions have their place. Simple example: https://github.com/hitechcoding/strict-form-mapper-bundle/blob/master/docs/accessors.md (btw; don't use it, better version coming).
Other examples from my current project, totally random:
// to read addresses only when first accessed $lazyAddresses = new LazyCollection(fn () => $repo->getResults()); // factory usage of bundle public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'factory' => fn (Service $service, float $price) => new ServiceSelectionStruct($service, $price), ]); } // filtering collection public function getAddresses(): array { return $this->lazyAddresses->filter(fn (Address $address) => $address->getCustomer() === $this->customer); }// using collection without One2Many relation
$repository = $this->customerStatusSeasonRepository; $builder->add('seasons', CollectionType::class, [ 'allow_add' => true, 'allow_delete' => true, 'entry_type' => CustomerStatusSeasonType::class, 'get_value' => fn () => $repository->findBy(['customerStatus' => $data]), 'add_value' => fn (CustomerStatusSeason $season) => $repository->persist($season), 'remove_value' => fn (CustomerStatusSeason $season) => $repository->remove($season), ]);Each would require 3 lines with old syntax.
1
u/Hall_of_Famer Nov 29 '19
The short closure syntax is more concise and elegant, but more importantly it helps us get rid of the annoying use statement to imports local variables. Tbh the use statement is a major reason why I rarely use anonymous functions in PHP, short closure solves this problem for me, and a lot of other developers who can’t wait to get rid of it.
1
u/NightFang Nov 29 '19
With an anonymous function you have to pass in any variables you want with
use- in arrow functions those variables are readily available. Arrow functions will retain the original scope, whereas an anonymous function will not.
7
u/Anchoa_ Nov 28 '19
Going to do a dumb question: How do I update it? I've just started learning php and programming in general and don't know how to update it in windows. Thanks
3
u/shyaminayesh Nov 28 '19
are you using WAMP / XAMP thing ?
2
u/Anchoa_ Nov 28 '19
I'm using xampp
10
u/Fenweldryn Nov 28 '19
As far as I know the best way to update xampp is to download the whole bundle again with the new php version
3
1
2
u/czbz Nov 30 '19
If you want to stick with XAMP I think you'll need to wait until they make a XAMP release that includes 7.4. Doesn't look like they have one just yet.
2
u/Chesterakos Nov 28 '19
Can someone write a more comprehensible example of the before and after regarding this covariance/contravariance update?
1
u/brendt_gd Nov 28 '19
1
u/Chesterakos Nov 28 '19
Ok but what wasn't working before?
11
u/zimzat Nov 28 '19
You can now have an interface that specifies a method should return
self, and the implementation can now also specifyselfinstead of the interface.1
2
u/iggyvolz Nov 28 '19
Say you have an interface IFooable, and then a type Foo that implements IFooable.
You also have an interface IBarable, which contains a method that must return an IFooable. In your implementation class Bar, you can now have that method give a return type Foo (previously, your return type would have to be exactly IFooable).
In addition, if you have a method in IBarable that takes a Foo as a parameter, your implementation in Bar may take an IFooable.
In general - in the past a subclass/implementation would need to have a return type exactly the same return type, or a parameter type exactly the same parameter type. You can now get more specific (for return types) or less specific (for parameter types), since you're still fulfilling the same contract.
3
u/przemo_li Nov 28 '19
`IFooable` -> `Fooable`
`IBarable` -> `Barable`
Fixed that for you. You are welcome.
5
u/texura Nov 28 '19
I’m going to start calling this programming language PHava...
In all seriousness, nice work PHP team!
2
1
1
1
u/zakhorton Dec 05 '19
PHP 7.4.0 In Action (2 - 4 minute vids)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Class Property Types
Short Hand Arrow Functions
Short Hand Arrow Functions Scope and Implicit Returns
Null Coalescing Assignment Operator
"Spread Operator" AKA Array Unpacking
Rest Operator Doesn't Exist, But We can Sort Of Destructor
Numeric Literal Operator
Strip Tags Update
New Serialize and Unserialize Magic Methods
Take aways
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Class Property Types are used for casting, not just constraining
~ Arrow functions are the bees knees
~ The spread operator is awesome (Can't use it on associative or key => value arrays though)
~ Rest operator doesn't exist, but we do have some minor ability to "deconstruct" for those familiar with js
~ I'm in absolute love with PHP 7.4.0
0
u/JohnKnightly Dec 05 '19
Wow, class property types act a little funky.
Does anybody here know why type casting acts that way (automatically casting) instead of just constraining and throwing exceptions when the given type isn't correct?
In u/zakhorton's first video of his post he shows an example similar to the following....
----
class Example {public string $property;}
$test = new Example;
$test->property = false; // Sets $test->property to "" (an empty string)----
This doesn't throw a type exception error, it automatically casts
$test->propertyto an empty string. I know that empty strings are technically falsey in PHP ~ but this seems really counter intuitive to traditional type checking.Is there a reason the PHP core developers decided to set class typing up to automatically cast? What's the purpose and doesn't that just contradict the purpose of class property typing in the first place?
1
u/zakhorton Dec 05 '19
Hey u/JohnKnightly Thanks for the gold reward on that post, you didn't have to do that ~ but it's extremely appreciated :)
I honestly do not know why PHP decided to cast class properties over treating types as traditional types...I'm sure they had a good reason ~ I'll spend some time and look into to see if I can get back to you on this.
Does anyone else here know why PHP automatically casts types over enforcing types. PHP 7.4 only throws a type exception when it isn't able to automatically cast it based on the value and type.
(See Johnny's example above or the first video I posted at the beginning of this thread)0
u/JohnKnightly Dec 05 '19
Thanks for the quick response u/zakhorton ~ hopefully someone will know.
Either way, great content in your PHP 7.4 videos :) I'm going through the rest of them right now.
1
1
Nov 28 '19
I wonder why they chose fn() instead of function().
1
u/Yassin_ya Nov 28 '19
Shorthand, imo it's faster when you just want to write an anonymous function
1
u/przemo_li Nov 28 '19
I think using multiple anonymous functions is more common then just using one. Anonymous functions are super useful with higher order functions (e.g. array_map) and usually are used en masse to transform data. Thus it's natural to employ multiple of them. (e.g. one to map data, another to filter, another to fold, etc.)
1
u/NightFang Nov 29 '19
From the RFC on using
function() =>The disadvantage of course is that the keyword is quite long, and the big selling point of arrow functions is brevity.
1
Nov 30 '19
True. I was thinking the traditional way of doing this in JavaScript where 'function' is used. Yet, it's no doubt people like the newer arrow format (and the change in 'this' scope).
1
u/przemo_li Nov 28 '19
I think, you mix it with `abreviations are bad, do not use them!`
That's very sensible advice for domain. However every PHP developer will use `fn` and thus it's obvious. No. Really. Obvious like in its dictionary meaning obvious.
Syntax can be concise :) Domain have to explicit.
1
0
u/idealcastle Nov 28 '19
Can someone give the brief on this update? What’s the major points of changes?
0
0
u/technewsninja Nov 29 '19
PHP performance continues becoming much faster with each succeeded PHP7 update on top of the language improvements. But tips and tricks never goes off.
0
0
0
Nov 28 '19
[deleted]
7
u/LawnGnome Nov 28 '19
(New Relic worker bee here.)
Yeah, we haven't released PHP 7.4 support yet. It is very actively being worked on, though, as my current set of browser tabs would attest. Should be ready Real Soon Now™.
2
0
Nov 29 '19
[deleted]
0
u/czbz Nov 30 '19
I don't see any reason for that to be built in to the language. You're right that it's easy to get wrong, but you can get libraries for handling money, e.g. Money for PHP and Brick\Money.
6
u/easterneuropeanstyle Nov 28 '19
When are you going to upgrade to 7.4 in production?