lundi 8 avril 2019

Implementing interface based on the chosen interface

So if I have an interface iConnection

interface iConnection
{
}

Which is implemented by:

class OutlookConnection implements iConnection
{
}

And

class GoogleConnection implements iConnection
{
}

And another interface iComparison

interface iComparison
{
}

Which is implemented by:

class OutlookComparison implements iComparison
{
}

And

class GoogleComparison implements iComparison
{
}

In the main program I want to be able to switch between GoogleComparison and OutlookComparison based on the iConnection type without using an if or switch statement:

public function __construct(iConnection $connection)
{
    $this->connect = $connection;
    if($this->connection instanceof GoogleConnection){
      $this->comparison = new GoogleComparison();
    }
    elseif($this->connection instanceof OutlookConnection){
      $this->comparison = new OutlookComparison();
    }
}

Is this possible to achieve within this constructor without switch or if statements?

Aucun commentaire:

Enregistrer un commentaire