PHP 目前已经有了面向对象功能,但为何如此重要的类似于 Java 的接口功能没有了呢?
回复内容:
PHP接口类interface的正确使用方法
PHP: 对象接口
what are you taking about?
PHP中有接口,它是看到什么新的东西就往自己的特性里面装,也不看适用不适用。最后搞得跟个静态语言似的。
个人认为接口的约束性太强,不适合动态语言。动态语言更适合一种“弱接口”的概念,即实现了某个方法就认为实现了某个接口。反正你动态语言方法调用相当于反射,干什么都是反射,那就用 method_exists
来判断好了。
动态类型的语言要interface何用
先问是不是,再问为什么。
PHP有traits。这都2014年了,也许interface应该被淘汰了,单从语言来讲的话。
也有interface。。。
何止有interface,还有trait,直接复用方法代码,不用每个类都实现一遍。也就是java用java 8里的interface default method才能实现的东西。
在php里主要用于运行时的type hinting。
但这个type hinting只能用clss或者interface,用int或者string之类是不行的,简直就是动态语言界的(哔哔哔),看到高大上的就硬塞进语言里,做不好又硬做,留个坑再慢慢补。
第一,是有Interface的!
第二,PHP的Interface并非完全没有用,动态类型的语言的确在某些面向对象特性的使用上表现乏力,但是PHP在类型检测上却有所体现,比如之前看的Laravel框架中的用法:
<code class="language-text">public function validateCredentials(UserInterface $user, array $credentials){ ... }
</code>
Stellungnahme:Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn