PHP Tutorial

PHP Examples

PHP Practice

PHP program to demonstrate the example of a simple interface

Here, we are going to demonstrate the example of a simple interface in PHP.
Submitted by Nidhi, on November 20, 2020 [Last updated : March 13, 2023]

Simple Interface Example

Here, we will create an interface that contains the declaration of functions and then implement the interface into a class with function definitions.

PHP code to implement a simple interface

The source code to demonstrate the example of a simple interface is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

<?php
//PHP program to demonstrate the example of 
//simple interface.
interface Inf
{
    public function fun1();
    public function fun2();
}

class Sample implements Inf
{
    public function fun1()
    {
        printf("fun1() called<br>");
    }
    public function fun2()
    {
        printf("fun2() called<br>");
    }
}

$obj = new Sample();

$obj->fun1();
$obj->fun2();
?>

Output

fun1() called
fun2() called

Explanation

In the above program, we created an interface Inf that contains the declaration of two functions fun1() and fun2().

After that we created a class Sample, here we used the implements keyword to implement an interface into the class, then we defined the functions fun1() and fun2() into the Sample class.

At last, we created the object of the Sample class and called functions that will print the appropriate message on the webpage.

PHP Class & Object Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.