P
P
Peter Gribanov2016-05-06 19:45:02
PHP
Peter Gribanov, 2016-05-06 19:45:02

How to clarify / specify the type of a method parameter when implementing an interface in PHP?

As far as I know, this cannot be done, but just in case, I ask because it hurts. I 'm using PHP 5.6
Problem:
There are 4 classes with very similar interface. I will give only 2

class Cover
{
    public function moveTmpFile(CoverInterface $entity)
    {
        // ..
    }

    public function removeOldFiles(CoverInterface $entity)
    {
        // ..
    }

    public function removeAllFiles(CoverInterface $entity)
    {
        // ..
    }
}

class OgImage
{
    public function moveTmpFile(OgImageInterface $entity)
    {
        // ..
    }

    public function removeOldFiles(OgImageInterface $entity)
    {
        // ..
    }

    public function removeAllFiles(OgImageInterface $entity)
    {
        // ..
    }
}

The CoverInterface and OgImageInterface interfaces are fundamentally different. That is, these services have a very similar interface, but different implementation. I would like to create a common interface for the Cover class and OgImage. If there is a single interface, then this will optimize the work with services. Ideally, it should look like this:
Basic interface for $entity:
interface UploadableInterface
{
}

Inherit it in CoverInterface and OgImageInterface
interface CoverInterface extends UploadableInterface
{
    public function getCover();

    public function setCover($filename);

    // ..
}

interface OgImageInterface extends UploadableInterface
{
    public function getOgImage();

    public function setOgImage($og_image);

    // ..
}

Basic interface for services:
interface UploaderInterface
{
    public function moveTmpFile(UploadableInterface $entity);

    public function removeOldFiles(UploadableInterface $entity);

    public function removeAllFiles(UploadableInterface $entity);
}

we inherit the interface with refinement / instantiation of the parameter type
class Cover implements UploaderInterface
{
    public function moveTmpFile(CoverInterface $entity)
    {
        // ..
    }

    public function removeOldFiles(CoverInterface $entity)
    {
        // ..
    }

    public function removeAllFiles(CoverInterface $entity)
    {
        // ..
    }
}

class OgImage implements UploaderInterface
{
    public function moveTmpFile(OgImageInterface $entity)
    {
        // ..
    }

    public function removeOldFiles(OgImageInterface $entity)
    {
        // ..
    }

    public function removeAllFiles(OgImageInterface $entity)
    {
        // ..
    }
}

Looks great, but it can't be done. PHP requires that the method accept only UploadableInterface and nothing else. The only solution to this problem is not to specify the type of the parameter at all, but this solution is no good.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-05-06
@ghost404

A class can implement several interfaces and no inheritance is needed to solve your problem:
Moreover, if you think carefully about what interfaces are and why they are needed, it becomes clear that the term "inheritance" does not apply to them in principle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question