【问题标题】:Generate PHP Interfaces生成 PHP 接口
【发布时间】:2012-01-05 01:22:46
【问题描述】:

是否有从现有类生成 php 接口的工具?如果有一个像 Netbeans 自动 getter/setter 创建但用于接口的工具,那就太好了。

【问题讨论】:

  • 你在说什么?接口的 Getter/Setter 没有任何意义,因为接口不能有属性。你应该澄清你的问题
  • @KingCrunch nice to have a tool like Netbeans automatic getter/setter creation Netbeans 提供“从类属性功能自动生成 getter 和 setter。他希望为接口提供类似的东西。“从类生成接口”
  • 啊,我明白了。 edorian 可能已经给出了答案,但通常(根据“按合同设计”)您应该始终首先拥有接口并根据它们的签名创建类。 (只是想提一下)

标签: php interface


【解决方案1】:

目前PHPStorm 8 可以做到这一点,也许之前的版本也可以。

步骤:

  1. 将光标放在类名上
  2. 选择:重构 -> 提取 -> 接口
  3. 填写选项并完成。

【讨论】:

    【解决方案2】:

    对于编程用法,InterfaceDistiller 允许您从现有类派生接口,如下所示:

    $distiller = new InterfaceDistiller;
    $distiller
        ->methodsWithModifiers(\ReflectionMethod::IS_PUBLIC)
        ->extendInterfaceFrom('Iterator, SeekableIterator')
        ->excludeImplementedMethods()
        ->excludeInheritedMethods()
        ->excludeMagicMethods()
        ->excludeOldStyleConstructors()
        ->filterMethodsByPattern('(^get)')
        ->saveAs(new SplFileObject('MyInterface.php'))
        ->distill('SomeFoo', 'MyInterface');
    

    它还有一个 CLI 界面:

    Usage: phpdistill [options] <classname> <interfacename>
    
      --bootstrap                           Path to File containing your bootstrap and autoloader
    
      --methodsWithModifiers <number>       A ReflectionMethod Visibility BitMask. Defaults to Public.
      --extendInterfaceFrom  <name,...>     Comma-separated list of Interfaces to extend.
      --excludeImplementedMethods           Will exclude all implemented methods.
      --excludeInheritedMethods             Will exclude all inherited methods.
      --excludeMagicMethods                 Will exclude all magic methods.
      --excludeOldStyleConstructors         Will exclude Legacy Constructors.
      --filterMethodsByPattern <pattern>    Only include methods matching PCRE pattern.
      --saveAs                              Filename to save new Interface to. STDOUT if omitted.
    

    我不知道有任何 IDE 为 php 提供此类功能。

    【讨论】: