Free cookie consent management tool by TermsFeed Generator PHP Interface | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Interface

PHP Interface

Last Updated on Mar 22, 2023

What is Interface?

Do you remember when we talked about Classes?

Interface is like a skeleton for the class. It doesn’t implement the functions. It just says what functions are needed. And what arguments should those functions get.

Let me show you an example

<?php
Interface Human{
    public function sayName();
    public function getAge(int $yearBorn);
}

As you can see we have not implemented those functions. We are just saying that if you are creating a human you should have these functions with those arguments.

Implement

Now if I want to create a class Man which is a human I tell php that I want to implement the functions in that skeleton

You’ll do this by using the word implements and the name of the interface

Class Man implements Human {
    
}

If you keep it like this, you will get an error. Php expects you to implement those functions so you have to do it.

Class Man implements Human
{
    public function sayName()
    {
        echo "my name is Amir";
    }
    public function getAge(int $yearBorn)
    {
        echo 'I am '.(date('Y') - $yearBorn).' years old';
    }
}

You can have other functions and other properties in that class. It doesn’t matter. As long as you implement the functions that have been specified in the interface you’re good to go.

Why Interface

You might say why on earth would I write the interface? I will write the class instead.

Well in small projects when you’re doing all the coding yourself it might not matter but when your project gets a little bigger or other developers start working on it as well, interface is your savior. It forces your code to keep organized and clean.

https://youtu.be/xEfPXG9sN9Q

Conclusion

Now you know about interface in PHP.

I recommend you to open a PHP files and create an interface. then define a class that implements that interface.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • what is interface
  • how to implement the interface
  • why use interface

Category: programming

Tags: #php #oop

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Loops
Mar 21, 2023 programming

PHP Loops

Today we are going to learn about loops in PHP. Loop is another important concept in programming and we are going to learn everything about loops in PHP. ...

11 Min Read Read More
PHP Interacting with Python
Mar 22, 2023 programming

PHP Interacting with Python

Today we are going to talk about interacting with a python file in PHP. Both PHP and Python are very powerful languages. you can run a python program from your PHP code. ...

6 Min Read Read More
PHP Dependency Management
Mar 22, 2023 programming

PHP Dependency Management

Today we are going to talk about dependency management in PHP. Your code might depends on some packages. You need a way to manage all the dependencies in PHP. ...

9 Min Read Read More
PHP Simple Web Crawler
Mar 22, 2023 programming

PHP Simple Web Crawler

Today we are going to build a simple web crawler in PHP and parse the box office movies from IMDB. We can do it with PHP easily. We don’t even need any extra package. ...

14 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...