PHP特性:好是坏?

tech2023-11-14  88

In early March 2012, the PHP Group announced the release of PHP 5.4. Developer eagerly anticipated the release because of the many new features 5.4 would bring, the most sought after being traits. In the build up to the release, Shameer C wrote a fantastic overview of using traits in PHP, and I highly recommend reading Shameer’s article before this one because I assume you have a basic understanding of traits.

2012年3月上旬,PHP集团宣布发布PHP 5.4。 由于5.4会带来许多新功能,而特性是最受追捧的内容,因此开发人员急切地期望该版本的发布。 在发行版的构建中,Shameer C撰写了关于在PHP中使用特征的精彩概述,并且我强烈建议在此之前阅读Shameer的文章,因为我认为您对特征有基本的了解。

Traits have been generally accepted by the PHP development community, mainly because it’s a feature that exists in other programming languages like Java, C++, and Phython. Additionally, the benefits of traits have been widely touted, with developers giving their own two cents on how traits can benefit any project, especially as a replacement for OOP inheritance. But are traits actually that good? Are they a feature which will help raise the level of PHP development, or are they just a fad?

特性已被PHP开发社区普遍接受,主要是因为它是Java,C ++和Phython等其他编程语言中存在的功能。 此外,特质的好处被广泛宣传,开发人员就特质如何使任何项目受益(特别是作为OOP继承的替代品)付出了自己的2美分。 但是特质真的那么好吗? 它们是一项有助于提高PHP开发水平的功能,还是只是一种时尚?

PHP特性不好 (PHP Traits are Bad)

On the surface, there is strong support for PHP traits because using them can help reduce code duplication throughout your application. In addition, they can help improve maintainability and the cleanliness of your code.

从表面上看,对PHP特性有强大的支持,因为使用它们可以帮助减少整个应用程序中的代码重复。 另外,它们可以帮助提高可维护性和代码的清洁度。

Traits are certainly welcomed, but many leading developers fear they may be used in ways that were never intended. Anthony Ferrara is one such developer, with his fear going so far as to contemplate the possibility of traits becoming the next most abused feature alongside eval and constants. But before Anthony makes his case, he raises a very interesting point: traits are actually a collection of specific mixins, which essentially don’t have state. PHP’s implementation of traits does allow states to be used, and therefore traits in PHP are actually mixins. This simple fact questions the true intent of PHP traits as ultimately they are flying under a false flag. There is no explanation regarding why traits are actually processed as mixins rather than the globally-acknowledged stateless mixins they should be.

特质当然受到欢迎,但是许多领先的开发人员担心它们可能会被以意想不到的方式使用。 安东尼·费拉拉(Anthony Ferrara)就是这样的一名开发人员,他担心甚至要考虑到特性可能与eval和常量一起成为第二个最常被滥用的特性 。 但在Anthony提出申诉之前,他提出了一个非常有趣的观点:特征实际上是特定的mixin的集合,而这些本质上没有状态。 PHP的traits实现确实允许使用状态,因此PHP中的trait实际上是mixins。 这个简单的事实质疑PHP特性的真实意图,因为它们最终在错误的旗帜下飞舞。 没有任何关于为何将特征实际上作为混合素而不是它们应该被全局认可的无状态混合素处理的解释。

Anthony continues, citing that traits are very close in functionality to what extends allow us to with regard to coupling classes together. With extends being a well-respected and long-used feature, it is only fair to ask whether traits truly have a place in PHP or are they trying to stand atop the shoulders of existing features in an attempt to look tall?

安东尼继续说,特征在功能上与扩展所允许的非常接近,这使我们能够将类耦合在一起。 扩展是一项受人尊敬且长期使用的功能,因此,公平地问一下特性在PHP中是否真正占有一席之地,或者它们是否试图站在现有功能的肩膀上以显得高大才是公平的?

Then there’s also the question of interfaces. Many developers have only a vague idea about what the real difference is between interfaces and traits; their ability to be reused is very similar, and the inheritance depth possible with them is also very similar. Are traits actually something new to PHP, or are they really just an upgrade to interfaces?

然后还有接口的问题。 对于接口和特征之间的真正区别,许多开发人员只有一个模糊的想法。 它们的重用能力非常相似,并且它们可能具有的继承深度也非常相似。 特性实际上是PHP的新特性,还是只是接口的升级?

PHP特质很好 (PHP Traits are Good)

Regardless of the unanswered questions, traits are great for PHP, allowing us to create multiple inheritance scenarios (extends only supports single inheritance).

不管悬而未决的问题,特质对于PHP都很有用,它使我们能够创建多个继承方案(扩展仅支持单个继承)。

Single inheritance has been used for many years now and comes part and parcel with object orientated programming in PHP, which has restricted advanced programmers from developing complex systems while keeping code clean and to a minimum. In other languages, multiple inheritance could be used to eliminate duplicate code in such situations. But multiple inheritance isn’t possible in PHP. Interfaces are instead offered as a substitute, and an unsuccessful one at that.

单一继承已经使用了很多年,并且与PHP中的面向对象的编程密不可分,这限制了高级程序员开发复杂的系统,同时又保持代码简洁且最小化。 在其他语言中,在这种情况下,可以使用多重继承来消除重复的代码。 但是在PHP中不可能多重继承。 取而代之的是提供接口,而接口则不成功。

Interfaces are not intended to be used in this way, rather to act as contracts which force any classes that implement it to deliver its functionality. This can help open up the coupling of classes and methods, but doesn’t offer a true substitute multiple inheritance in PHP.

接口并非旨在以这种方式使用,而是充当合同,强制实施该接口的任何类提供其功能。 这可以帮助打开类和方法的耦合,但是不能在PHP中提供真正的替代多重继承。

A few developers have attempted to create some creative solutions for multiple inheritance in PHP, but many are bloated, making the overall result redundant and more of an experiment than a practical solution.

一些开发人员试图为PHP中的多个继承创建一些创造性的解决方案,但是许多开发人员却ated肿了,使整体结果变得多余,并且比实际解决方案更具实验性。

With traits, multiple inheritance can be implemented naturally using ingenious systems. As Shameer showed in his introduction to traits, you can create multiple traits inside classes. John Squibb uses the multiple traits to create a multiple inheritance environment in his 2011 example.

使用特质,可以使用巧妙的系统自然地实现多重继承。 正如Shameer在特质介绍中所展示的那样,您可以在类内部创建多个特质。 John Squibb 在其2011年的示例中使用多重特征创建了多重继承环境。

Many programmers think multiple inheritance is evil citing the Diamond Problem, and claim single inheritance provides less headaches. The fact of the matter with PHP isn’t the practicality of implementing multiple inheritance but rather the symbolism that comes with the possibility of implementing multiple inheritance. This has become ever more important in light of many of the programming public stating their dislike for PHP, evident if you google “don’t use PHP”. With the possibility of multiple inheritance, PHP becomes a more challenging, more expressive, and more acceptable programming language, rather than just the “most common web language”. I find this incredibly comforting when Java, Python, or C++ programmers try to dismiss PHP because of its lack of support of standardized programming methods across languages.

许多程序员以钻石问题为由认为多重继承是邪恶的,并声称单一继承可以减轻麻烦。 PHP的问题所在不是实现多重继承的实用性,而是实现多重继承的可能性所带来的象征意义。 鉴于许多编程人士都说他们不喜欢PHP,这一点变得越来越重要,如果您用Google搜索“不要使用PHP”,这一点就显而易见。 有了多重继承的可能性,PHP成为更具挑战性,更具表现力和可接受性的编程语言,而不仅仅是“最常见的Web语言”。 当Java,Python或C ++程序员尝试关闭PHP时,我发现这令人难以置信,因为它缺乏跨语言的标准化编程方法的支持。

Traits are a true indication that PHP is slowly and surely become a language which will embrace more and more standard programming techniques, hopefully winning over many more established programmers and developers.

特质确实表明PHP正在缓慢而肯定地成为一种语言,它将包含越来越多的标准编程技术,希望能赢得更多成熟的程序员和开发人员的青睐。

结论 (Conclusion)

Traits allow PHP developers to create cleaner, simpler, and more efficient code while also allowing more complex systems to be made and experimented with. They’re not good; they are fantastic! They open up another level of development techniques to OOP programmers using PHP, and I believe they are a sign of things to come in future PHP versions.

特性使PHP开发人员可以创建更简洁,更简单和更高效的代码,同时还可以创建和试验更复杂的系统。 他们不好。 他们太棒了! 它们为使用PHP的OOP程序员提供了另一种开发技术水平,我相信它们是将来PHP版本中即将出现的事情的标志。

For more information on the topics mentioned in this article, including a low down on traits themselves, please see the following links:

有关本文提到的主题的更多信息,包括对特征本身的了解,请参阅以下链接:

Ircmaxell’s Blog: Are Traits The New Eval?

Ircmaxell的博客:特性是新的评估吗?

John Squibb’s Programming Blog: Multiple Inheritance in PHP 5.4 using Traits

John Squibb的编程博客:PHP 5.4中使用特性的多重继承

AzonMedia: Multiple Inheritance in PHP – a Workaround

AzonMedia:PHP中的多重继承–解决方法

Refulz PHP: Traits in PHP 5.4 – Why we need Traits

Refulz PHP:PHP 5.4中的特征–为什么我们需要特征

Christer’s Blog o’Fun: Playing with Traits in PHP 5.4alpha1

Christer的博客o'Fun:玩PHP 5.4alpha1中的特征

Slideshare: PHP Traits, Treat or Threat?

幻灯片分享:PHP是特质,对待还是威胁?

Refulz PHP: Traits in PHP – Multiple and Nested Traits

Refulz PHP:PHP中的特征–多重和嵌套特征

Image via Fotolia

图片来自Fotolia

翻译自: https://www.sitepoint.com/php-traits-good-or-bad/

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)