happy cat image

everdevel

coding

login
NotificationX
  • Currently, only notices of comments are displayed.
  • no comment or please login

Interface

Interfaces are used when you need to define rules for using classes.
The rules here mean what methods should be declared in which classes and which properties should be declared.
You may be wondering why you need to do this annoying thing.
It doesn't matter much if you do something alone by yourself.
But usually a company works on a project with multiple people.
If this person touches it without a plan, it is difficult to manage.
Now that the times are better, it's called code configuration management program, but in the past, it was a good time to lie if someone went wrong.
Anyway, for some reason or another, it's time for us to need a class usage convention for administrative issues. In fact, I have never used it in practice.

How To Use Interface

interface interface name
{
}

For example, if the interface name is howto and you need to create the run(), stop(), and turn() methods in your class, write:

<?php

    interface howto
    {
        public function run();

        public function stop();

        public function turn();

    }
?>

To apply an interface to a class, use the implements keyword.

How to Use implements

class class name implements interface name
{
}

Then shall we try?

<?php

    interface howto
    {
        public function run();

        public function stop();

        public function turn();

    }

    class Car implements howto
    {
        public function run()
        {
            return 'run.';
        }

        public function stop()
        {
            return 'stop.';
        }

        public function turn()
        {
            return 'turn.';
        }
    }

    $hello = new Car;
    echo $hello->run();
?>

Result

You may not know why you use the interface?
Then let's not use it in a class that is declared in an interface.
Below we will not use the turn() method in the class.

<?php

    interface howto
    {
        public function run();

        public function stop();

        public function turn();

    }

    class Car implements howto
    {
        public function run()
        {
            return 'run.';
        }

        public function stop()
        {
            return 'stop.';
        }
    }
    $hello = new Car;
?>

Result

The interface told me to use the turn() method, but the class doesn't declare the turn() method, which causes an error.
Conversely, using a method in the class that is not declared in the interface is fine.
It's going to be updated every time, but it's hard to manage if you have to put it in the interface every time.
Fortunately, it is designed to specify the required methods.
Let's check if it works by putting methods in the class that are not specified in the interface.

<?php

    interface howto
    {
        public function run();

        public function stop();

        public function turn();

    }

    class Car implements howto
    {
        public function run()
        {
            return 'run.';
        }

        public function stop()
        {
            return 'stop.';
        }

        public function turn()
        {
            return 'trun.';
        }

        public function auto()
        {
            return 'auto.';
        }
    }

    $hello = new Car;
    echo $hello->turn();
?>

Result

If you are working on a project with multiple people and you use the interface when a rule occurs, it may be useful to avoid errors in advance.
You can also inherit the ^-^ * interface. Next time, we will learn about interface inheritance.


Thank you for visiting. If you have any inquiry or explanation of mistakes, please use the comments below.


    
    

Back to the course

ALL COMMENTS 0

Sort by