from Hacker News

PHP 8.4

by theThree on 11/21/24, 9:56 AM with 323 comments

  • by idoubtit on 11/21/24, 11:06 AM

    I'm just a PHP programmer for work, but I worry about the orientation PHP has chosen. As French people say: better is the enemy of good (Le mieux est l'ennemi du bien). The two new language features bring a higher language complexity for dubious gains. I hope I won't have to work with these.

    Property hooks mean that some language magic will turn a property access into a call to methods. It implies that `$this->x` has a different meaning if it's inside a hook or outside hooks. I've used this kind of feature (getters/setters) with JS code (and with Moose/Perl decades ago), and I wasn't convinced. Plain methods are more explicit, have less cognitive charge, and are easier to extend.

    On the bright side, I'm glad that the language is still thriving. In 2021, I was worried when the foundation was created, especially as I read that Nikita Popov had left. He was the creator of PHP's JIT code, and at the time the only developer who could fully understand it. But it seems there was no need to worry. PHP is now longer "the elephant in the room" of web programming, but it's still a good language, with many active developers at its core.

  • by eurleif on 11/21/24, 8:36 PM

    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 relevant hook. This is true for all hooks on the same property. This includes, for example, writing to a property from the get hook; that will write to the backing value, bypassing the set hook.
       
       A normal property has a stored “backing value” that is part of the object, and part of the memory layout of the class. However, if a property has at least one hook, and none of them make use of $this->[propertyName], then no backing value will be created and there will be no data stored in the object automatically (just as if there were no property, just methods). Such properties are known as “virtual properties,” as they have no materialized stored value.
       
       Be aware, the detection logic works on $this->[propertyName] directly at compile time, not on dynamic forms of it like $prop = 'beep'; $this->$prop. That will not trigger a backing value.
    
    Feels like too much magic to me that a property access can mean different things depending on context, but I'm not a PHP user, so I don't get a vote.
  • by acabal on 11/21/24, 5:58 PM

    I'm most excited for property hooks. Having getters and setters as part of the language syntax was something I dearly missed from my C# days, nearly two decades ago.

    In my projects I sometimes emulate getters and setters using `__get()` and `__set()` but that's heavy-handed and requires lots of PHPDoc annotation for type checking. Property hooks look awesome!

  • by tored on 11/21/24, 12:44 PM

    Great PHP release. Better stack traces for closures, performance improvements (always nice), HTML5 support, lazy objects and much more. Great work and a big thanks to everyone involved!
  • by LeftHandPath on 11/21/24, 9:02 PM

    I went to look at array accessor overloading today and saw “Property Hooks” in the sidebar (under “Classes and Objects”).

    I didn’t know what they were, so I clicked. I was bewildered that I had never run into them before, used them in my own code, or seen them used by others. Come to find out they’ve only been around for about a day!

    Reminds me of some of the lovely expressibility and syntactic sugar that’s pulled me to other languages lately. Glad to see it make its way into PHP.

  • by bornfreddy on 11/21/24, 8:26 PM

    Am I the only one excited about bcmath objects? Not so much because of arithmetic operators, but mostly because now we can do data types checks without resorting to wrapper classes. Nice!
  • by nneonneo on 11/21/24, 11:42 AM

    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, $filter_callback) while the older array_search is ($search_value, $array).

    There are literally hundreds of hits for existing, global, functions named array_find. If these are loaded from a library, they will break uses of the new built-in function in exciting ways. Yet, even with this mentioned in the RFC (https://wiki.php.net/rfc/array_find) it seems to have been no obstacle whatsoever to its inclusion - despite the fact that the implementation is literally three lines of code.

    I have to question if the benefits of this global function really outweigh the benefits. PHP devs claim that other languages have these functions so PHP should too - but neglect to note that most languages don’t make them global functions (rather, they’re usually array methods or in some utility module).

  • by cbg0 on 11/21/24, 4:51 PM

    Glad to see PHP still chugging along after all these years. It's the language I started with as a freelancer more than a decade ago and I still remember having books on my desk learning the proper way to do things, as you had to work around all the unsafe things the language would let you do and which unfortunately led to it getting a bad rep.
  • by littlestymaar on 11/21/24, 5:48 PM

    I find it pretty fascinating that what used to be a beginner-friendly language, with limited capabilities but that is very easy to get started with, has now evolve to a bloated monster full of advanced features that you can't expect to know entirely, with a complex framework and tooling ecosystem to support it.

    PHP lovers generally don't like acknowledge that, but the PHP we've learned back-end development two decades ago is no more and that it's now as beginner unfriendly as Java was when we picked it.

    It's a pity because there's nothing as beginner-friendly anymore. I think the blame is on people calling themselves “PHP developers” who never bothered learning more advanced languages after PHP and instead pushed PHP to reach parity with those, at the expanse of losing its soul.

  • by theodorejb on 11/21/24, 12:01 PM

    Property hooks are the headline feature, but they seem like something I'd rarely use in practice. It is nice to have the option available though, in case I need to add extra logic to a property without breaking everywhere that it's accessed.
  • by inglor_cz on 11/21/24, 10:31 AM

    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?
  • by PaulHoule on 11/21/24, 6:12 PM

    That “public private(set)” boggles my mind. Why not “readonly public”?
  • by klaussilveira on 11/22/24, 12:00 PM

  • by trevor-e on 11/21/24, 5:31 PM

    The property access is looking a lot like Swift, particularly the `private(set)` part which I haven't seen in many other languages.
  • by arkh on 11/22/24, 8:40 AM

    > Object API for BCMath

    Really good thing when doing some operations on monetary values. No need to use bc_* functions anymore it seems.

  • by shikck200 on 11/22/24, 4:34 AM

    Looks like PHP is going even further towards Java. I really loath getters/setters with passion. I really wish PHP instead focused on getting some sort of concurrency builtin and proper unicode string literals. That should be the main focus, instead of copying various features from Java.
  • by cannibalXxx on 11/22/24, 7:35 AM

    even though it's a much criticized language I still build my applications using it. for example the site https://chat-to.dev was written entirely in php
  • by KRAKRISMOTT on 11/21/24, 4:59 PM

    Are there any solid PHP WebSockets and WebRTC setups outside of Laravel?
  • by nilslindemann on 11/22/24, 9:03 AM

    Non PHP expert here, can someone explain to me the line

    get => \sprintf("%s_%s", $this->languageCode, $this->countryCode);

    in the first code example? Is it a lambda?

  • by justinclift on 11/22/24, 7:55 AM

    Did they skip 8.4.0 and just go to 8.4.1 directly?
  • by radicalriddler on 11/21/24, 7:33 PM

    The new Dom stuff looks really cool. I had to work with the previous functions a couple months ago and it was terrible. New API looks nice.
  • by that_guy_iain on 11/21/24, 10:21 PM

    The breaking changes section doesn’t even mention they changed error handling in methods that haven’t been change for 20 years.
  • by cute_boi on 11/21/24, 5:29 PM

    I can see PHP is getting too much complex.
  • by TheRealPomax on 11/21/24, 7:46 PM

    Standards compliance for HTML is huge. Hopefully everyone switches to it ASAP.
  • by vfclists on 11/21/24, 4:51 PM

    For how many years will it get support, security and bug fixes?
  • by chx on 11/21/24, 5:16 PM

    oooo

    variable-length lookbehind assertions are now supported.

    yay! I needed that so many times.

  • by anttihaapala on 11/21/24, 10:55 AM

    Oftentimes many of the significant new PHP features are to fix the shortsighted implementation in the previous ones - for example this method chaining with `new` - there was precedent already, C++ got it right well before PHP even existed and with the very same arrow operator that PHP borrowed (and so did Java and JavaScript with .), so the question is why did PHP have to get it wrong at first and for so long.

    Another pet peeve of me is that the global namespace is littered with these utility functions that should be easily composable or maybe be methods on the objects themselves - and looks like PHP 8.4 adds four more `array_*` functions. For comparison, Python's builtin namespace has a total of 71 functions and a couple of exception classes. PHP's builtin namespace has more functions for dealing with arrays and now 58 of those are prefixed with `array_`.

  • by mg on 11/21/24, 10:41 AM

    These days, I am super torn about what language to use for new web projects.

    PHP:

        - Easy to deploy: Upload files, done.
        - Easy to develop: Reload page, see changes.
        - Lots of HTTP tooling built in.
        - Fast.
    
    Python:

        - Great language.
        - Great code reuse system: Modules.
        - Nice framework: Django.
        - Less breaking changes in recent years.
  • by hasnain99 on 11/21/24, 9:10 PM

    I don't Use Php but I think learning Php its better than to learn Typscript
  • by reconvene1290 on 11/21/24, 10:47 AM

    > PHP 8.4 is a major update of the PHP language.

    No it’s not: it’s a MINOR update.