PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP AdvancedPHP Include and RequirePHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP OOP IntroductionPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP InterfacesPHP OOP TraitsPHP OOP ConstantsPHP OOP StaticPHP OOP NamespacesPHP OOP Autoloading

PHP OOP Interfaces

Interfaces allow you to specify what methods a class should implement.
PHP OOP Interfaces
PHP OOP Interfaces

If you know what are abstract classes, you already know interfaces. There is only a little difference between those. Therefore, we will learn interfaces comparing to abstract classes.

Why Interfaces?

When you need to let a class to compulsorily implement some methods in it, you can use interfaces. This is a good practice when creating real-world web applications in PHP.

If you need to support the child classes by adding some non-abstract method, you should use abstract classes. Otherwise, interfaces would be your choice.

Declaring Interfaces

Interfaces are declared using the Interface keyword.


<?php
Interface MyFirstInterface { ... }

Only Abstract Methods

An interface can only have method signatures. It means that all the methods cannot have a body - only the declaration.

And, all the methods must have public visibility.


<?php
Interface MyInterface {
	public function myMethod1();
	public function myMethod2($name, $age);
	public function myMethod3() : int;
}

Implements Keyword

Unlike abstract classes, we use the implements keyword to implement an interface in a child class.


<?php
class MyClass implements MyInterface { ... }

After implementing an interface in a class, all the abstract methods in the interface should be declared in the class.

All the abstraction rules apply when using interfaces. Here's the link to the rules if you don't remember them.

As all the abstract methods in an interface have public visibility, all the methods in the class that implements the abstract methods should have public visibility.

Interface Example

Let's recreate the example we created in the last chapter using interfaces. Now, you will understand the difference between interfaces and abstract classes.

Interface


Interface Person {
	public function __construct($name);
	public function greet() : string;
}

Implemented Class


class Programmer implements Person {
	public $name;
	public function __construct($name) {
		$this -> name = $name;
	}
	public function greet() : string {
		return "Hello World from " . $this -> name;
	}
}

Full Code

PHP Interface Example


<?php
Interface Person {
	public function __construct($name);
	public function greet() : string;
}

class Programmer implements Person {
	public $name;
	public function __construct($name) {
		$this -> name = $name;
	}
	public function greet() : string {
		return "Hello World from " . $this -> name;
	}
}
$programmer = new Programmer('John');
echo $programmer -> greet();

Run Example ››

Implementing Multiple Interfaces

A class can implement multiple interfaces separated by commas in the declaration (After the implements keyword). If you do that, you have to declare all the abstract methods within the class.

PHP Implementing Multiple Interfaces


<?php
Interface MyInterface1 {
	public function myMethod1();
}
Interface MyInterface2 {
	public function myMethod2();
}
class MyClass implements MyInterface1, MyInterface2 {
	public function myMethod1() {
		echo "Hello ";
	}
	public function myMethod2() {
		echo "World";
	}
}
$obj = new MyClass();
$obj -> myMethod1();
$obj -> myMethod2();

Run Example ››

Extends + Implements

A class can extend a class and also implement one or more interfaces.

PHP Extending and Implementing


<?php
Interface MyInterface {
	public function write();
}
class ParentClass {
	public $name;
	public function __construct($name) {
		$this -> name = $name;
	}
}
class ChildClass extends ParentClass  implements MyInterface {
	function write() {
		echo $this -> name;
	}
}
$child = new ChildClass('Hyvor');
$child -> write();

Run Example ››

Remember, you can only extend one class even you could implement multiple interfaces.

Extending Interfaces

Interfaces can also extend interfaces.

PHP Extending Interfaces


<?php
Interface MyInterface1 {
	public function myMethod1();
}
Interface MyInterface2 extends MyInterface1 {
	public function myMethod2();
}
class MyClass1 implements MyInterface1 {
	# only this method is needed
	public function myMethod1() {}
}
class MyClass2 implements MyInterface2 {
	# both myMethod1 and myMethod2 should be declared
	public function myMethod1() {}
	public function myMethod2() {}
}

Facebook Twitter