简体   繁体   English

(何时)我应该在PHP中使用类型提示?

[英](When) should I use type hinting in PHP?

I can't understand the motivation of PHP authors to add the type hinting. 我无法理解PHP作者添加类型提示的动机。 I happily lived before it appeared. 在它出现之前我很高兴地活着。 Then, as it was added to PHP 5, I started specifying types everywhere. 然后,当它被添加到PHP 5时,我开始在任何地方指定类型。 Now I think it's a bad idea, as far as duck typing assures minimal coupling between the classes, and leverages the code modularization and reuse. 现在我认为这是一个坏主意,只要鸭子类型确保类之间的最小耦合,并利用代码模块化和重用。

It feels like type hints split the language into 2 dialects: some people write the code in static-language style, with the hints, and others stick to the good old dynamic language model. 感觉就像类型提示将语言分成两种方言:有些人用静态语言风格编写代码,有提示,有些人则坚持使用良好的旧动态语言模型。 Or is it not "all or nothing" situation? 还是不是“全有或全无”的情况? Should I somehow mix those two styles, when appropriate? 在适当的时候,我应该以某种方式混合这两种风格吗?

It's not about static vs dynamic typing, php is still dynamic. 这不是静态与动态类型,PHP仍然是动态的。 It's about contracts for interfaces. 这是关于接口的合同。 If you know a function requires an array as one of its parameters, force it right there in the function definition. 如果您知道一个函数需要一个数组作为其参数之一,请在函数定义中强制它。 I prefer to fail fast, rather than erroring later down in the function. 我更喜欢快速失败,而不是稍后在函数中出错。

(Also note that you cant specify type hinting for bool, int, string, float, which makes sense in a dynamic context.) (另请注意,您无法为bool,int,string,float指定类型提示,这在动态上下文中是有意义的。)

You should use type hinting whenever the code in your function definitely relies on the type of the passed parameter. 只要函数中的代码肯定依赖于传递参数的类型,就应该使用类型提示。 The code would generate an error anyway, but the type hint will give you a better error message. 无论如何代码都会生成错误,但类型提示会为您提供更好的错误消息。

The motivation of the PHP group to add type hinting was to provide people used to Java-style OOP with another feature that would make the platform more familiar, comfortable, and appealing. PHP小组添加类型提示的动机是为那些习惯于Java风格的OOP的用户提供另一个功能,使平台更加熟悉,舒适和吸引人。 This, in turn, would make PHP more "enterprise ready", which would help Zend with their core business. 反过来,这将使PHP更加“企业就绪”,这将有助于Zend的核心业务。

Marketing aside, it does have its uses. 除了营销,它确实有其用途。 If you're writing a method that operates on a parameter in a specific way that would cause unintended (often silent) failures if the parameter was something else, then using type hinting ensures the code will break during development rather than breaking in production. 如果您正在编写一个以特定方式对参数进行操作的方法,如果该参数是其他方式,则会导致意外(通常是静默)失败,那么使用类型提示可确保代码在开发期间中断,而不是在生产中中断。

If you ever decide doing type hinting at least do it using interfaces instead of concrete or abstract classes. 如果您决定使用接口而不是具体或抽象类来进行类型提示。 The reason is simple, PHP does not allow multiple inheritance but does allow implementing multiple interfaces. 原因很简单,PHP不允许多重继承,但允许实现多个接口。 So if anyone tries to use your library it won't have difficulties implementing your interface as opposed to the case where he would have to extend your abstract/concrete class given that he already extends another already. 因此,如果有人试图使用你的库,那么实现你的界面就不会有困难了,相反,他必须扩展你的抽象/具体类,因为他已经扩展了另一个。

Without type hinting it would be impossible for the IDE to know the type of a method parameter and thus provide the proper intellisense - your editor does have intellisense, right? 如果没有类型提示,IDE将无法知道方法参数的类型,从而提供正确的智能感知 - 您的编辑器确实具有智能感知,对吧? ;). )。 It should be said that I just assume IDEs use this for intellisense, as this is the first I've heard of type hinting in PHP (thanks for the hint btw). 应该说我只是假设IDE使用它来进行智能感知,因为这是我第一次听说PHP中的类型提示(感谢提示btw)。

Type hinting is a point of debate at our company (mostly the Java people like it), and I am a very old school PHP programmer (and program in other languages). 类型提示是我们公司的一个争论点(主要是像这样的Java人),我是一个非常老派的PHP程序员(和其他语言的程序)。

My advice is to avoid type hinting and to include try/catch handlers in each complex function. 我的建议是避免类型提示并在每个复杂函数中包含try / catch处理程序。

Type hinting forces an application to rely on the callers exception handling environment which is in general bad and goes untested, the primary problem. 类型提示强制应用程序依赖于调用者异常处理环境,这通常是坏的并且未经测试,这是主要问题。 For web apps, this results in the white screen of death, for batch it results in simply a fatal exit without logging good messages in most cases, and you sitting their scratching your head trying to recreate the user or application problem that management is on your back to resolve. 对于网络应用程序,这导致死亡的白色屏幕,对于批处理而言,它导致致命的退出而在大多数情况下没有记录好的消息,并且您试图重新创建管理对您的用户或应用程序问题。回来解决。

Local exception handling provides for a more controlled testing scenario including garbage in data types and garbage in data values, which gives a much more complete testing suite compared to a difficult to test exception handling path in the caller by passing in an incorrect type and expecting the exception. 本地异常处理提供了一个更受控制的测试场景,包括数据类型中的垃圾和数据值中的垃圾,与通过传入不正确的类型并期望调用者中难以测试的异常处理路径相比,它提供了更完整的测试套件。例外。

The exception testing also fails in many cases due to stack version problems (ie some versions of PHP like 5.4 do not catch "catchable fatal" errors in a proper way and ergo phpunit simply dies breaking testing suites. This is a stack specific problem, however in my experience type hinting simply is unnecessary, makes people that are used to a typed language accept PHP better without realizing the impact, and causes much more complex testing scenarios (very difficult to test the callers handling exception path results). 由于堆栈版本问题,异常测试在许多情况下也失败了(例如,某些版本的PHP(如5.4)没有以正确的方式捕获“可捕获的致命”错误,并且ergo phpunit只是破坏了测试套件。这是一个特定于堆栈的问题,但是根据我的经验,类型提示简直是不必要的,使得习惯于键入语言的人更好地接受PHP而不会产生影响,并导致更复杂的测试场景(很难测试处理异常路径结果的调用者)。

The Java and other typed language folks are not accepting of or understanding how to leverage and benefit from the default mixed type parameters in PHP... They will learn someday but only if they embrace the PHP way. Java和其他类型的语言人员不接受或理解如何利用PHP中的默认混合类型参数并从中受益......他们将在某一天学习,但前提是他们采用PHP方式。 ;-) ;-)

Best lessons are learned when developing robust PHP unit based testing scenarios, and that will normally shed light on why type hinting is a pain in the butt related to testing, and causes much more problem than good... To each their own, and my apps run better and more reliably and testing turn out much more complete with generally 100% code coverage including catch paths in local functions. 在开发基于PHP单元的强大测试场景时,可以学到最好的经验教训,这通常可以解释为什么类型提示是与测试相关的屁股的痛苦,并导致更多的问题而不是好...对于他们自己和我的每一个应用程序运行得更好,更可靠,测试结果更加完整,通常100%的代码覆盖率,包括本地功能中的捕获路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM