Interfaces



object interfaces ( interface ) are used to prototype a class, or to force this interface-bound class to contain methods that the interface contains. The most common and important use is to create a common interface for different classes that will contain the same methods. Thus, with species imperatives, samples of different classes can be transmitted to sub-methods through a common interface. They are designed as classes, but use the interface name instead of the class expression . When included in classes, they are also included with the implements statement. A class may contain more than one interface at the same time.

 

 

 

The characteristics of Objective Qualities are;

Can only be used with classes.
Multiple interfaces can be accommodated within a class.
● They can not be sampled as classes.
The methods in it have to have public visibility and no artwork.
● They should be the same or narrower than the methods included in the classroom they are included in.
Create a common interface for multiple classes with the same methods.

 

 

# Syntax


The syntax of object interfaces is similar to the syntax of classes, but the  class interface is used  instead of the class interface

<?php interface ExampleClassInterface
{
    # Yöntemler public görünürlüğe sahip ve gövdesiz olmalıdır.
    public function exampleFunction();
}

 

 

# Create Interface (Interface)


interfaces are created with a context as shown in the syntax above.

File: VehicleInterface.php
<?php interface VehicleInterface
{
    public function startEngine(array $connection);

    public function setColor(string $color);

    public function getColor() : String;
}

 

 

# Use the Object Interface


In class we will use our interface file for the examples above, we create  the implements we will include specifying the statement.

File: Vehicle.php
<?php class Vehicle implements VehicleInterface
{
    protected $color = 'White';
 
    public function startEngine(array $connection)
    {
        return $connection;
    }

    public function setColor(string $color)
    {
        $this->color = $color;
    }

    public function getColor() : string
    {
        return $this->color;
    }
}
Fatal Errors

A class with an interface has to include all methods in the relevant interface individually. Otherwise it will give a fatal error.

<?php interface ExampleInterface
{
    public function tests();
}

class Example implements Example
{
    public function test()
    {
        return true;
    }
}
Fatal error : Class Automobile contains 1 abstract method and must be declared abstract or implement the following methods ( AutomobileInterface :: tests) in  C: \ xampp \ htdocs \ test.php  on line  8

Furthermore, the methods involved in the class have to be the same or broader in scope.

<?php interface ExampleInterface
{
    public function test(string $a);
}

class Example implements ExampleInterface
{
    public function test()
    {
        return true;
    }
}
Fatal error : Class Automobile contains 1 abstract method and must be declared abstract or implement the following methods ( AutomobileInterface :: tests) in  C: \ xampp \ htdocs \ test.php  on line  8
Multiple Interface Usage

 More than one interface can be included in a class.

<?php interface ExampleInterface
{
    public function example();
}

interface OtherInterface
{
    public function other();
}

class Example implements ExampleInterface, OtherInterface
{
    public function example()
    {
        
    }

    public function other()
    {

    }
}