A PHP interface allows you to specify what methods a class should implement. Interfaces make it easy to use a variety of classes in the same way, ensuring that they all implement the same methods.
An interface is defined using the interface
keyword. All methods declared in an interface must be public.
interface Animal {
public function makeSound();
}
A class implements an interface by using the implements
keyword. The class must define all the interface's methods.
class Dog implements Animal {
public function makeSound() {
echo "Bark";
}
}
A class can implement multiple interfaces, separating them with commas. This allows for flexible and reusable code structures.
interface CanFly {
public function fly();
}
class Bird implements Animal, CanFly {
public function makeSound() {
echo "Chirp";
}
public function fly() {
echo "Flying";
}
}
Interfaces can extend other interfaces, similar to class inheritance. This allows interfaces to be more modular and specific.
interface Mammal extends Animal {
public function walk();
}
class Human implements Mammal {
public function makeSound() {
echo "Speak";
}
public function walk() {
echo "Walking";
}
}
Interfaces can be used as type hints in function definitions, ensuring that the passed objects implement the required interface.
function makeAnimalSound(Animal $animal) {
$animal->makeSound();
}
$dog = new Dog();
makeAnimalSound($dog);
While both abstract classes and interfaces can enforce method implementation, interfaces cannot have properties or method implementations, offering more flexibility in design.
abstract class AbstractAnimal {
abstract public function makeSound();
}
class Cat extends AbstractAnimal {
public function makeSound() {
echo "Meow";
}
}
Using interfaces with dependency injection allows for more flexible and testable code, as dependencies can be swapped easily.
class Zoo {
private $animal;
public function __construct(Animal $animal) {
$this->animal = $animal;
}
public function hearAnimal() {
$this->animal->makeSound();
}
}
$bird = new Bird();
$zoo = new Zoo($bird);
$zoo->hearAnimal();
In large applications, interfaces help in defining clear contracts between different parts of the application, making the codebase more maintainable.
interface Logger {
public function log($message);
}
class FileLogger implements Logger {
public function log($message) {
file_put_contents('log.txt', $message, FILE_APPEND);
}
}
class Application {
private $logger;
public function __construct(Logger $logger) {
$this->logger = $logger;
}
public function run() {
$this->logger->log("Application started");
}
}
$logger = new FileLogger();
$app = new Application($logger);
$app->run();
Interfaces are often used in real-world applications like payment gateways, where different payment methods can implement a common interface.
interface PaymentGateway {
public function pay($amount);
}
class PayPal implements PaymentGateway {
public function pay($amount) {
echo "Paying $amount using PayPal";
}
}
class Stripe implements PaymentGateway {
public function pay($amount) {
echo "Paying $amount using Stripe";
}
}
function processPayment(PaymentGateway $gateway, $amount) {
$gateway->pay($amount);
}
$paypal = new PayPal();
processPayment($paypal, 100);
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies