Answer the question
In order to leave comments, you need to log in
How to get autocomplete for extended implementation based on interface/abstract class/class?
There is a library, it works with the object. The dependency is injected so that clients can write their own implementations of . The problem is that the autocomplete is for the specified type, and even if we wrote our own implementation with an extended api, we break off the autocomplete. How is this generally solved?
UPD. We can change the TaxiService implementation as we like, but when we put it in open source, then, of course, clients should not change the source code in order to get their goodies (autocomplete for their implementations).
interface Machine
{
public function go();
}
class Car implements Machine
{
public function go()
{
// TODO: Implement go() method.
}
}
class ElectroCar extends Car
{
public function charge()
{
}
}
class TaxiService
{
protected $machine;
function __construct(Machine $machine)
{
$this->machine = $machine;
}
public function getMachine()
{
return $this->machine;
}
}
$taxi = new TaxiService(new ElectroCar());
$taxi->getMachine()->// как получить charge ?
Answer the question
In order to leave comments, you need to log in
Autocomplete follows phpdoc.
If the option of specifying phpdoc in the code itself does not suit you, you can separate classes / methods without implementation but documented, for example like this:
class TaxiService {
/**
* @var Machine
*/
protected $machine;
function __construct(Machine $machine)
{
}
/**
* @return Machine
*/
public function getMachine()
{
}
}
/**
* @var Machine|OtherInterface|OtherClass
*/
protected $machine;
As an option:
$taxi = new TaxiService(new ElectroCar());
/** @var ElectroCar $taxiElectroCar */
$taxiElectroCar = $taxi->getMachine();
$taxiElectroCar->
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question