class Test impliments DataInterface
{
public function data(array $data): string
{
return implode('-', $data);
}
}
class Test2 impliments DataInterface
{
public function data(array $data): string
{
return join('+', $data);
}
}
interface DataInterface
{
public function data(array $data): string;
}
class MyClass
{
// @var DataInterface
private $object;
public function __construct(DataInterface $object)
{
$this->object = $object
}
public function getData(array $data) : string
{
return $this->object->data($array);
}
}
$test1 = new Test();
$test2 = new Test2();
$obj = new MyClass($test);
$obj2 = new MyClass($test2);
echo $obj->getData([1,2,3]);
echo $obj2->getData(['a', 'b', 'c']);