Live data from Hacker News

PHP 8.4

php.net

231–240 of 337 posts

Re: PHP 8.4

#231

Earlier quoted context omitted.

I'm curious, why do you like getters and setters? I know the textbook answer is so that every single possible property can become some mutable chain of events so you can swap names or side-effects without the caller knowing, but I've yet to find a use for that in real life. It just leads to builder patterns and other oddities like forced decorators like Java has everywhere. I felt like beans were the realization that…

The more I program the more I realize that we are all blind men feeling out an elephant. It sounds like you got the gist but somehow you are in an area of programming where getter/setters aren't useful. That's fine and okay. Part of growing up as a programmer is realizing your niche. Most of the advice out there is deep, not broad. It's deeply connected with our niche and not necessarily broadly applicable.

Getters/setters are just as easily associated with hidden behavior that makes systems worse.

The only sibling comment of yours that bothered to even show code just demonstrated how they turned user.lastPost into a method that makes a hidden DB query.

Yet the benefits are supposedly so nuanced and hard to get across that merely asking about it gets a "you'll understand when you get some real experience ;)" comment from you.

Re: PHP 8.4

#232

If you want any evidence that terrible language design is alive and well in PHP, look no further than the new array_find function. Not only is it yet another global function in a namespace already chock full of random array helpers, it is extremely similar in both name and usage to array_search - a global function since PHP 4. Except, of course, that in typical PHP fashion, array_find’s argument order is ($array, $fi…

array_find parameter ordering is modeled after other non-variadic array functions, e.g. array_filter also having ($array, $callback) order.

But I agree, there is already some inconsistency, and when adding a new function you have to choose which function to make it consistent and which one to make it inconsistent with.

Re: PHP 8.4

#233

That “public private(set)” boggles my mind. Why not “readonly public”?

readonly is rather a misnomer for "writeonce" in PHP. And as such it also disallows repeated assignment in class scope, while private(set) has no such restriction.

Re: PHP 8.4

#234
post #112

oooo variable-length lookbehind assertions are now supported. yay! I needed that so many times.

While variable-length lookbehind is surely more comfortable to work with, I found clever usage of \K to be largely sufficient in most cases.

But I agree, a great feature!

Re: PHP 8.4

#235
post #229

I was curious about why setting `$this->countryCode` inside the setter for `countryCode` didn't result in infinite recursion. Turns out this is spelled out in the RFC, but not in the docs: When a hook is called, inside that hook $this->[propertyName] will refer to the “unfiltered” value of the property, called the “backing value.” When accessed from anywhere else, $this->[propertyName] calls will go through the relev…

C# has the same thing as Properties, but the backing value is set explicitly as a separate variable: https://learn.microsoft.com/en-us/dotnet/csharp/programming-...

There are many times I would love to have an automatic backing field in C#. C# will do that if you use the default setter but in cases where that's not possible I dislike having to both declare a separate field and having it available to the rest of the class outside of the setter.

Re: PHP 8.4

#236
post #229

Earlier quoted context omitted.

C# has the same thing as Properties, but the backing value is set explicitly as a separate variable: https://learn.microsoft.com/en-us/dotnet/csharp/programming-...

There are many times I would love to have an automatic backing field in C#. C# will do that if you use the default setter but in cases where that's not possible I dislike having to both declare a separate field and having it available to the rest of the class outside of the setter.

https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/cs...

edit: the properties reference now documents the 'field' keyword too

Re: PHP 8.4

#237
post #229

Earlier quoted context omitted.

C# has the same thing as Properties, but the backing value is set explicitly as a separate variable: https://learn.microsoft.com/en-us/dotnet/csharp/programming-...

There are many times I would love to have an automatic backing field in C#. C# will do that if you use the default setter but in cases where that's not possible I dislike having to both declare a separate field and having it available to the rest of the class outside of the setter.

C# 13 has this via the field keyword (as preview feature): https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...

It's pretty similar to what PHP provides here, except that PHP uses "$this->propname =" and C# uses "field =".

Edit: As someone involved in the RFC, it's somewhat funny, because we considered a special variable like "$field" or "$value" too magic, and C# does just that with a field keyword.

Re: PHP 8.4

#238

I have a question to the PHP-in-production crowd: how long do you wait before migrating to higher version of PHP? Is the first release usually already fine, or is it better to wait for a few months and let someone else catch the early errors?

We leave it on staging for about 4 months, scan logs etc. If there is no complaints from qa or devs we roll it out.

Re: PHP 8.4

#239

I was curious about why setting `$this->countryCode` inside the setter for `countryCode` didn't result in infinite recursion. Turns out this is spelled out in the RFC, but not in the docs: When a hook is called, inside that hook $this->[propertyName] will refer to the “unfiltered” value of the property, called the “backing value.” When accessed from anywhere else, $this->[propertyName] calls will go through the relev…

The dynamic form is really not needed very often. Should it ever become a problem triggering a loop you can just set and get from a private property with a different name. But then also 90% of getters and setters are really simple so it will feel as magic as gravity.

Re: PHP 8.4

#240

Earlier quoted context omitted.

There are many times I would love to have an automatic backing field in C#. C# will do that if you use the default setter but in cases where that's not possible I dislike having to both declare a separate field and having it available to the rest of the class outside of the setter.

https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/cs... edit: the properties reference now documents the 'field' keyword too

That might be all the motivation I need to go move to .NET 9.
Post reply on HN