3

I have been programming PHP for half year now, I am into Object-Oriented for 3 months. But I have never used Abstracted class or a Interface.

One of my mates explained, but in Java:

If you want to give a certain class a structure, like implementing an interface as Entity, all underlying classes must inherit the methods from the interface and give it a body.

But I still don't understand, in PHP. I can't find any moment to use the Interface.

When do people usually find Interfaces useful? Are they useful? Or I can live without them? Can you show me a perfect example of a right moment to use a interface?

I've read in the Documentation, but didn't understand either.

Another question:

If I want to use different files for interfaces, for example:

Body.interface.php

To use that interface in the specific class, do I have to use include(), right?


3 답변


1

Interfaces are a set of instructions for your class. When you implement an interface, the inheriting class must follow the rules defined by the interface.

Interfaces include a list of all methods and properties that your inherited class must have, their arguments, types and any PHP documentation. They serve two key purposes: provide a set of instructions for a developer who is extending the parent class, and a set of rules for the extended class to follow.

A great scenario for use is if your creating a class that will be accessed or expanded in the future by other classes. In my case, I use Interfaces often when creating APIs, like Restful or SOAP APIs. The base class is concrete and contains some important required tools, so the interface tells my other developers how they can interact with the parent class.

A Valid use of Interface

interface MyInterface {
    /**
     * This method is required in your inheriting class
     * @var \stdClass $MyRequiredParameter - A object of options
     */
    public function myRequiredMethod(stdClass $MyRequiredParameter);
}

class MyClass implements MyInterface {
    public function myRequiredMethod(\stdClass $MyRequiredParameter)
    {
        return $MyRequiredParameter->MyValue;
    }
}

class MyExtension extends MyClass implements MyInterface {
    public function myRequiredMethod(\stdClass $MyRequiredParameter)
    {
        return $MyRequiredParameter->MyOtherValue;
    }
}

Example of an Invalid Extension of a Interfaced Class

class MyBadExtension implements MyInterface {
    public function weDontNeedNoStinkingRequiredMethods()
    {
        return FALSE;
    }
}

Since we didnt follow the pattern of the interface, we generate a fatal exception:

Fatal error: Class MyBadExtension contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (MyInterface::myRequiredMethod)


  • So basically the point of a interface, is set rules for a class? so we can only use one public function (according to your example) named myRequiredMethod with 1 parameter, am I correct? It's just the body of the class, like my mate explained? - Jony Kale
  • Interface methods always have to be public. Protected and Private methods can only be accessed from inside the the class and therefore are not an 'interface'. - phpisuber01

0

Interfaces are designed to help you specify behavior that does not fit in the class inheritance tree but that should be available using specific method calls. Think of it as an ability.

If you can count something, it is countable and you could implement the Countable interface. And some of those things also can be sorted, they are sortable. You could define your own interface for that and force methods for both ascending and descending sorting.

An interface does not have implementations for any of its methods, you are forced to do that in your implementing class.

You need to include interfaces in the same way you include (abstract) classes. The easiest way is to use an autoloader and have php load the classes and interfaces the moment it needs them.


0

  • Interface is same as questions, when the interface is created and it is implemented with the class then class has to use the all the methods of interface. interface are short, means the methods are declared not defined. the class can use external(own ) methods.
  • interface can be extended
  • all the methods has to be public in interface
  • interface is useful if you are Bose and you want to tell your clients to use this methods or various types of methods, simply just write or create interface then let your client to implement in their class

to include interface in class you have to create a interface;

 interface interface_name{
        public function submodule1();
        public function submodule2();
}

now implement or apply it in class

class class_name implements interface_name{
public function submodule1()
{
//some code
}
public function submodule2()
{
//some code
}
}

Linked


Related

Latest