What is the purpose of interfaces in php?

If I define an interface in PHP and a factory class that creates an instance of this interface, is there a way to force client code to use an interface rather than a base concrete class? In my opinion, clients can actually use any public functions / fields in the base class.

Here is an example:

<?php interface IMyInterface { public function doSomething(); } ?> <?php class ConcreteImplOfMyInterface implements IMyInterface { const NotPartOfInterface = 'youcantseeme'; public function doSomething() { } } ?> <?php class MyInterfaceFactory { public static function createMyInterface() { return new ConcreteImplOfMyInterface(); } } ?> <?php function client() { $myInterface = MyInterfaceFactory::createMyInterface(); return $myInterface::NotPartOfInterface; } ?> 
+8
php interface
source share
4 answers

What you are asking for is only possible with a statically typed language, and since PHP is dynamically typed, the short answer is that this is not possible.

For example, in Java, createMyInterface can return IMyInterface , and the only possible operations on this object are those defined in the interface itself. Of course, the object really has the ConcreteImplOfMyInterface type, so you can always apply it to this type to access other fields / methods.

PHP has no declared types, so what you return from a function is just a "variable" - it has no type. And since there are no types, all searches for fields / methods are dynamic, so you can always access anything that is "there" in the object.

In a sense, the interfaces are really somewhat limited in use in a language such as PHP - any class that implements the interface must implement all its methods, but since there is no guarantee that the function can return in the first place, there are essentially no guarantees at all . The best you can do is use instanceof to check if an unknown variable implements this interface.

+6
source share

This is why interfaces are good in PHP:

 interface Printable{ public function display(); } function display(Printable $obj){ $obj->display(); } 

This requires that any variable included in this function is an instance of Printable. Thus, you force anyone who uses your code to implement this interface if you want to use this function.

PHP 5.1 http://php.net/manual/en/language.oop5.typehinting.php

If you want to make sure that someone else cannot use certain methods or class variables, you need to define these methods and variables as private . This will make them unable to use anything but this particular class. (Even classes extending your class can use them)

+9
source share

Since PHP is a dynamic language, this means that all calls to class members, etc. will be allowed at runtime. This means that if I have code that calls $ foo-> bar () and I get an object in $ foo that has a method called bar (), then it will be called, even if it may not be a method bar (), which I intended to call but some other method on some other object, which coincidentally is called the same.

However, you can provide some protection using typed parameters so that the function accepts only certain objects:

 function doingSomething(IMyInterface $foo) { $foo->doSomething(); } 

However, you still cannot prevent anyone from executing IMyInterface and then write functions that accept IMyInterface, but use them as if they were an object of some higher class. As I said, this is because PHP is a dynamic language.

+2
source share

Make private methods.

 return $myInterface.NotPartOfInterface; 

It works? I do not think it is necessary. I think you need a :: operator.

. used only for string concatenation.

+1
source share

Source: https://habr.com/ru/post/649885/


All Articles