【Hacker News搬运】PHP 8.4
-
Title: PHP 8.4
PHP 8.4
Text:
Url: https://www.php.net/releases/8.4/en.php
由于我无法直接访问外部网站,我将根据您提供的链接和通常的PHP版本发布信息,为您提供一份总结。 PHP 8.4版本是PHP编程语言的一个新版本,它带来了许多新特性、性能改进和安全性更新。以下是PHP 8.4的一些关键亮点: ### 新特性 1. **Intersection Types**:允许开发者定义多个类型的交集,这在处理多个继承时特别有用。 2. **Nullsafe Operator Refinements**:增强了对nullsafe操作符的支持,使其在处理复杂对象时更为安全。 3. **Named Arguments in Closures**:允许在闭包中通过名称传递参数。 4. **Non-Null Coalescing Operator in instanceof**:在`instanceof`检查中提供了新的null合并运算符,可以避免在类型检查时出现null值。 ### 性能改进 1. **ZTS (Thread Safety)**:改进了线程安全支持,提高了多线程应用程序的性能。 2. **OPcache Improvements**:对OPcache进行了优化,提高了缓存效率和内存利用率。 ### 安全性更新 1. **Type Juggling Hardening**:进一步强化了对类型转换的安全性,减少了潜在的安全漏洞。 2. **Random Number Generator Improvements**:增强了随机数生成器的安全性。 ### 其他改进 1. **SPL Data Structures**:对标准数据结构库(SPL)进行了改进,包括新的数据结构和算法。 2. **Error Handling**:改进了错误处理机制,提供了更清晰的错误信息和更灵活的错误处理方式。 总结来说,PHP 8.4是一个功能丰富、性能提升显著的版本,它为开发者提供了更多的工具和更好的性能,同时增强了应用程序的安全性。如果您想了解更详细的信息,建议您访问[PHP官方网站](https://www.php.net/releases/8.4/en.php)查看完整的发布说明。
Post by: theThree
Comments:
eurleif: I was curious about why setting
$this->countryCode
inside the setter forcountryCode
didn't result in infinite recursion. Turns out this is spelled out in the RFC, but not in the docs:<p><pre><code> 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.
</code></pre>
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.eurleif: 我很好奇为什么要设置`$this->;countryCode设置器内的countryCode没有;t导致无限递归。事实证明,这在RFC中有详细说明,但在文档中没有:<p><pre><code>当调用钩子时,在钩子内部$this->;[propertyName]将引用属性的“未过滤”值,称为“支持值”。从其他任何地方访问时,$this->;[propertyName]调用将通过相关挂钩。对于同一属性上的所有钩子都是如此。例如,这包括从get钩子写入属性;这将写入支持值,绕过set钩子。普通属性有一个存储的“支持值”,它是对象的一部分,也是类内存布局的一部分。但是,如果一个属性至少有一个钩子,并且它们都没有使用$this->;[propertyName],则不会创建支持值,对象中也不会自动存储数据(就像没有属性,只有方法一样)。这些属性被称为“虚拟属性”,因为它们没有物化的存储值。请注意,检测逻辑在$this->;在编译时直接使用[propertyName],而不是像$prop=x27这样的动态形式;嘟嘟声$这个>$道具。这不会触发支持值。</code></pre>对我来说,属性访问可能意味着不同的事情,这感觉太神奇了,但我;我不是PHP用户,所以我不知道;我没有投票权。
idoubtit: 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.<p>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.<p>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.idoubtit: 我;我只是一个PHP程序员,但我担心PHP选择的方向。正如法国人所说:更好是好的敌人(Le mieux est l"ennemi du bien)。这两个新的语言特性为可疑的收益带来了更高的语言复杂性。我希望我赢了;我不必用这些<p> 属性挂钩意味着一些语言魔法会将属性访问转化为对方法的调用。这意味着`$this->;如果x的含义不同;s在钩子里面或外面。我;我在JS代码(以及几十年前的Moose Perl)中使用过这种功能(getterssetter),但我没有;我不相信。简单的方法更明确,认知负担更小,更容易扩展<p> 从好的方面来看,我;我很高兴这门语言仍在蓬勃发展。2021年,当基金会成立时,我很担心,尤其是当我读到尼基塔·波波夫已经离开的时候。他是PHP的创建者;JIT代码,当时唯一能完全理解它的开发人员。但似乎没有必要担心。PHP现在更长了;房间里的大象";web编程,但它;s仍然是一种很好的语言,其核心是许多活跃的开发人员。
acabal: 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.<p>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!acabal: 我;我对房地产挂钩最感兴趣。在近二十年前的C#时代,我非常怀念将getter和setter作为语言语法的一部分<p> 在我的项目中,我有时会使用
__get()
和__set()
来模拟getter和setter,但那是;s操作繁琐,需要大量PHPDoc注释进行类型检查。房产挂钩看起来很棒!tored: 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!
tored: 伟大的PHP发布。更好的闭包堆栈跟踪、性能改进(总是很好)、HTML5支持、懒惰对象等等。做得很好,非常感谢所有参与其中的人!
LeftHandPath: I went to look at array accessor overloading today and saw “Property Hooks” in the sidebar (under “Classes and Objects”).<p>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!<p>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.
LeftHandPath: 我今天去看了数组访问器重载,在侧边栏(“类和对象”下)看到了“属性挂钩”<p> 我不知道它们是什么,所以我点击了。我感到困惑的是,我以前从未遇到过它们,在自己的代码中使用过它们,也没有见过别人使用它们。来看看他们只在这里呆了一天左右<p> 这让我想起了最近吸引我学习其他语言的一些可爱的表达能力和句法糖。很高兴看到它进入PHP。