D
D
Danila2015-07-30 23:27:07
PHP
Danila, 2015-07-30 23:27:07

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

3 answer(s)
W
wol_fi, 2015-07-31
Ridzhi @Ridzhi

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()
    {
    }
}

And just specify such files in the IDE as an external library. Thus, for example, they solve the problem with autocomplete in phalcon.

M
Mikhail Osher, 2015-07-30
@miraage

/**
 * @var Machine|OtherInterface|OtherClass
 */
protected $machine;

S
Sergey Petrov, 2015-07-31
@sergoslav_0

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 question

Ask a Question

731 491 924 answers to any question