Uncategorized

PHP OOP Interfaces

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();

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();

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();

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() {}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Close