8

Is there a tool the generate php interface from existing classes? It would be nice to have a tool like Netbeans automatic getter/setter creation but for interfaces.

3
  • 2
    What you are talking about? Getters/Setters for interfaces don't make any sense, because interfaces cannot have properties. You should clarify your question Commented Nov 23, 2011 at 18:50
  • 1
    @KingCrunch nice to have a tool like Netbeans automatic getter/setter creation Netbeans offers a "autogenerate getters and setters from the classes properties feature. He wants something like that for interfaces. "Generate interface from class" Commented Nov 23, 2011 at 18:58
  • Ah, I see. Probably edorian already gives an answer, but usually (according "design-by-contract") you should always have the interfaces first and create the classes according their signatures. (Just wanted to mention this) Commented Nov 23, 2011 at 19:05

2 Answers 2

15

For programmatic usage there is InterfaceDistiller that allows you to derive interfaces from existing classes like this:

$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');

It also has a CLI interface:

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.

I'm not aware of any IDE that offers such functionality for php.

Sign up to request clarification or add additional context in comments.

Comments

4

Currently PHPStorm 8 can do this, maybe prior versions as well.

Steps:

  1. Place your cursor on the class name
  2. Choose: Refactor -> Extract -> Interface
  3. Fill out the options and done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.