Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate PHP Interfaces

Tags:

php

interface

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.

like image 365
orourkedd Avatar asked Nov 23 '11 18:11

orourkedd


People also ask

What are PHP interfaces?

A PHP interface defines a contract which a class must fulfill. If a PHP class is a blueprint for objects, an interface is a blueprint for classes. Any class implementing a given interface can be expected to have the same behavior in terms of what can be called, how it can be called, and what will be returned.

Why do we create interface in PHP?

The interface is another property of PHP that enables the developer to build a program without any complex methods. It inherits the same public method that a class holds. Due to these properties, an interface can define methods.

Can PHP implement multiple interfaces?

Classes may implement more than one interface if desired by separating each interface with a comma. A class can implement two interfaces which define a method with the same name, only if the method declaration in both interfaces is identical.

How do you declare an interface?

To declare an interface, use the interface keyword. It is used to provide total abstraction. That means all the methods in an interface are declared with an empty body and are public and all fields are public, static, and final by default.


1 Answers

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.

like image 77
edorian Avatar answered Sep 25 '22 00:09

edorian