happy cat image

Start Web Coding

Your First step toward web coding,
Start Web Coding

change UI

interface inheritance

When you inherit an interface, you use the extends keyword just like a class.

How to Use Interface Inheritance

interface Child Interface Name extends Parent Interface Name
{
}

Let's look at the code

<?php

    interface parentinter
    {
        public function auto();
    }

    interface childinter extends parentinter
    {
        public function run();

        public function stop();

        public function turn();

    }

    class Car implements childinter
    {

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

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

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

    }

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

In the code above, the Car class follows the interface childinter.
This is true for childinter's rules, but the childinter interface inherits it
parent interface, and parentinter parent has auto(). So you need to declare method auto() in childinter.
But it did cause an error.
Result of the code above

Result

In the code above, let's see if we add an auto() method to the Car class to see if an error occurs.

<?php

    interface parentinter
    {
        public function auto();
    }

    interface childinter extends parentinter
    {
        public function run();

        public function stop();

        public function turn();

    }

    class Car implements childinter
    {

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

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

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

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

    }

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

Result

강좌로 돌아가기