What is the meaning of interfaces in a weakly typed language, such as PHP?

I could never figure it out. If your language does not check type, what are the benefits of interfaces to you?

+6
php interface
source share
9 answers

Interfaces make your program fail earlier and more predictably when the subclass forgets to implement an abstract method in its parent class.

In traditional PHP OOP technology, you must rely on the following to execute a runtime error:

class Base_interface { function implement_me() { assert(false); } } class Child extends Base_interface { } 

With the interface, you get immediate feedback when one of your subclasses of the interface does not implement such a method during the declaration of the subclass, and not later during its use.

+10
source share

Taken from this link (summarizes):

  • Interfaces allow you to define / create a common structure for your classes - to set a standard for objects.
  • Interfaces solve the problem of single inheritance - they allow you to enter “qualities from multiple sources.
  • Interfaces provide a flexible base / root structure that you do not use to get with classes.
  • Interfaces are great when you have several coders working on a project; You can create a free structure for programmers to follow and let them worry about the details.
+4
source share

I personally find the pairing of a neat solution when building a DataAccess layer, which should support multiple DBMSs. Each DBMS implementation must implement the DataAccess global interface with functions such as Query, FetchAssoc, FetchRow, NumRows, TransactionStart, TransactionCommit, TransactionRollback, etc. Therefore, when you expand your data access capabilities, you are forced to use the generic defined function chhema so that you’re not interrupting the application at some point because you decided that the Query function should now be called execQuery.

Interaction helps you grow into a bigger picture :)

+2
source share

Types serve three different functions:

  • design
  • documentation
  • actual type checking

The first two do not require any type checking at all. Thus, even if PHP has not tested the interfaces, they will still be useful for only two reasons.

For example, I always think about my interfaces when I do Ruby, even though Ruby has no interfaces. And I often want to have a way to write these design decisions in source code.

On the other hand, I saw a lot of Java code that used interfaces, but obviously the author never thought about them. In fact, in one case, one could see from the indentation, spaces, and some remaining comments in the interface, which the author actually just copied and pasted the class definition and deleted all the method bodies.

Now to the third question: PHP actually does interface type checking. Just because it checks them at runtime does not mean that it does not print them at all.

And, in fact, it does not even check them at runtime; it checks them at boot time, which happens before execution. And doesn't "type checking happen at runtime, but before that" pretty much the very definition of static type checking?

+1
source share

You get errors if you do not add the required methods with the same signature.

0
source share

Interfaces that are often used with unit testing (test design).

It also offers you more stable code. Interfaces are also used to support iterators (for example, foreach support on objects) and comparators.

0
source share

It can be weakly typed, but there is a type for methods: function myFunc(MyInterface $interface) In addition, the interfaces help in testing and decoding the code.

0
source share

The type of hint in the function / method signatures allows you to have much more control over how the class interacts with it.

If you only hope that the user of your class will use only the right objects as method parameters, you are likely to run into problems. To prevent this, you will have to implement complex checks and filters that simply inflate your code and, of course, will reduce the performance of your codes.

The hint type gives you a tool to ensure compatibility without any bloated manual checks. It also allows your classes to tell the world what they can do and where they will fit.

Especially in complex frameworks such as the Zend Framework, interfaces make your life a lot easier because they tell you what to expect from the class and because you know what methods to implement must be compatible with something.

0
source share

In my opinion, there is no sense, there is no need and sense. Things like interfaces, visibility modifiers, or type hints are designed to ensure that a program is “right” (in a sense) without actually running it. Since this is not possible in a dynamic language such as php, these constructs are practically useless. The only reason they were added to php is more like java, which makes the language more attractive to the "corporate" market.

Forgot to add: uncommented downvoting sucks .//

-4
source share

All Articles