A
A
Alexey Markov2018-11-16 11:22:40
PHP
Alexey Markov, 2018-11-16 11:22:40

Is the php interface somehow tied to its implementation?

Good day.
I occasionally encounter constructions like

ClassController {
  public function actionIndex(CacheItemInterface $cache) {
    $cache = Request::post('input');
  }
}

What are these calls for and what exactly happens in them?
In Laravel, such a construction can mean calling a specific implementation of a given interface bound through a service.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2018-11-16
@orbitroom

No, not tied. These are Liskov Substitution Principle and Interface Segregation Principle aka the letters L and I in SOLID .

What are such calls for and what exactly happens in them?
These are not calls , but type hints - type hints for the interpreter. They are needed only so that an exception is thrown at runtime if an argument of an incorrect type was passed to the function.

A
Antony Tkachenko, 2018-11-16
@LemonFox

A class can implement (implements) an interface.
The interface tells us what methods the class should have, without directly implementing them (this is the responsibility of the class). In this example, there can be several classes that implement CacheItemInterface
, so that you can implement several cache providers (file cache, memcache, etc.)
php.net/manual/en/language.oop5.interfaces.php
cache is reassigned to some value.
There must be something rather

$data = $cache->get('key'); // При этом метод get должен быть указан в интерфейсе.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question