by theThree on 11/21/24, 9:56 AM with 323 comments
by idoubtit on 11/21/24, 11:06 AM
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
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
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
by LeftHandPath on 11/21/24, 9:02 PM
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
by nneonneo on 11/21/24, 11:42 AM
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
by littlestymaar on 11/21/24, 5:48 PM
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
by inglor_cz on 11/21/24, 10:31 AM
by PaulHoule on 11/21/24, 6:12 PM
by klaussilveira on 11/22/24, 12:00 PM
https://github.com/rectorphp/rector/issues/8701
https://github.com/nikic/PHP-Parser/commit/7b0384cdbe03431c4...
by trevor-e on 11/21/24, 5:31 PM
by arkh on 11/22/24, 8:40 AM
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
by cannibalXxx on 11/22/24, 7:35 AM
by KRAKRISMOTT on 11/21/24, 4:59 PM
by nilslindemann on 11/22/24, 9:03 AM
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
by radicalriddler on 11/21/24, 7:33 PM
by that_guy_iain on 11/21/24, 10:21 PM
by cute_boi on 11/21/24, 5:29 PM
by TheRealPomax on 11/21/24, 7:46 PM
by vfclists on 11/21/24, 4:51 PM
by chx on 11/21/24, 5:16 PM
variable-length lookbehind assertions are now supported.
yay! I needed that so many times.
by anttihaapala on 11/21/24, 10:55 AM
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
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
by reconvene1290 on 11/21/24, 10:47 AM
No it’s not: it’s a MINOR update.