In your case, what you are looking for is an Abstract Class , not an Interface.
I think it's important to make a brief distinction between Interfaces and Abstract Classes, since you seem to have some doubt in the difference between the operation of both. Remembering, both are concepts of Object Oriented Programming.
I will illustrate using the Java language, but the operation is similar in PHP:
Interfaces
They work as contracts signed between you and your class, where you present certain guarantees of class functioning; all classes that implement a certain interface must implement all methods (functions) declared in the interface, or the code will not compile. How methods are implemented, however, is not important, and there may even be an empty body of method.
The Caelum handout on Object Orientation brings
public interface Conta {
public double getSaldo();
public void deposita(double valor);
public void saca(double valor);
public void atualiza(double taxaSelic);
}
Next, we can create a class AccountCurrent that will implement the Account interface, thus ensuring that it will have all the basic methods of an Account, regardless of its specialty.
class ContaCorrente implements Conta {
// outros atributos e métodos
public double getSaldo() {...}
public void deposita(double valor) {...}
public void saca(double valor) {...}
public void atualiza(double taxaSelic) {...}
}
class ContaPoupanca implements Conta {
// outros atributos e métodos
public double getSaldo() {...}
public void deposita(double valor) {...}
public void saca(double valor) {...}
public void atualiza(double taxaSelic) {...}
}
The interesting thing about Interfaces is that you can create contracts with multiple interfaces, thus indicating that your class should perform functions linked to different concepts. For example, a Dog class can implement the Animal, Biped, and Mammalian interfaces. This is the way some programming languages use to implement Multiple Inheritance .
It is important to cite, however, that each class that implements the interface must declare its own implementation of the methods.
Abstract Classes
In short, they work like blueprints , that is, working templates of a class. Abstract Classes are one of the basic concepts of Inheritance . Abstract Classes can not be initialized by itself, and there should always be a concrete class that inherits from the abstract class. Abstract classes allow the declaration of variables and standard methods for all classes that inherit from it. Thus, taking the example of bank accounts mentioned above, you could declare standard data for all accounts (variables) and standard implementations of declared methods:
Note: For ease of example, I'll be using double to declare values, but you should not use double or float for values that require great precision because subsequent calculations with the variable precision can change its value along the way, unless you use methods to normalize values and reduce this variance.
public abstract class Conta {
private double saldo;
private String nome;
private String sobrenome;
private int idade;
public double getSaldo() {return this.saldo;}
public String getNome() {return this.nome;}
public String getSobrenome() {return this.sobrenome;}
public int getIdade() {return this.idade;}
public void deposita(double valor) {this.saldo += valor;}
public void saca(double valor) {
if (saldo > valor)
saldo -= valor;
else System.out.println("Não é possível sacar esse valor");
}
public void atualiza(double taxaSelic) {}
}
If when implementing an interface the class is required to create its own implementation of the declared methods, when inheriting from an abstract class, the methods declared in the abstract class are already readily usable by the class that inherited it. The concrete class takes on the role of daughter, who can use everything that has been declared in her father.
Imagine your father has a car. The car is not yours, but you, as a child, can use the car to leave occasionally.
public class ContaPoupanca extends Conta {
public ContaPoupanca (double saldo, String nome, String sobrenome, int idade) {
this.saldo = saldo;
this.nome = nome;
this.sobrenome = sobrenome;
this.idade = idade;
}
public void atualiza(double taxaSelic) {
this.saldo *= 1 + taxaSelic;
}
}
Unlike Interfaces, a class can inherit directly from only one other class. There is no way for a Dog class to inherit directly from Animal, Beep, and Mammal, since each of these classes may have method declarations with identical signatures, but different implementations, which would cause conflicts during program compilation.