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

how to use git restore and completely ignore the changes: 3 scenarios (video)
Nov 27, 2022 programming

how to use git restore and completely ignore the changes: 3 scenarios (video)

we are going to talk about ways to ignore the edits we made in a git project and restore the changes by git restore ...

9 Min Read Read More
Introduction to PHP and how to build your first web application in less than 10 minutes
Mar 21, 2023 programming

Introduction to PHP and how to build your first web application in less than 10 minutes

PHP is an open source scripting language for backend development. and we are going to build our very first PHP application together. ...

7 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
Node.js Beginner to Advanced 2025: Everything You Need to Know to Build Modern Projects
Jul 20, 2025 programming

Node.js Beginner to Advanced 2025: Everything You Need to Know to Build Modern Projects

Node.js has matured into one of the most essential tools in modern web development. Originally released in 2009, Node.js transformed JavaScript from a strictly frontend language into a robust backend solution capable of powering APIs, real-time applications, microservices, IoT systems, and more. ...

38 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 ...