Jump to content


Photo

Interfaces with type declaration


  • Please log in to reply
2 replies to this topic

#1 NotionCommotion

NotionCommotion
  • Members
  • PipPipPip
  • Advanced Member
  • 1,601 posts

Posted 11 May 2018 - 02:39 PM

I am sure I am missing some big concept, and would appreciate some help.

 

DogFactory::create()  must be given a DogPrototype object and must return a Dog object, and similarity for CatFactory::create().

 

The create() method is identical for both a dog or a cat, and I don't wish to duplicate the script in both of these two classes.

 

So, I came up with the following, but it results in a fatal error because "the class implementing the interface must use the exact same method signatures as are defined in the interface" as documented in http://php.net/manua....interfaces.php.

 

This seems like a good use, but since by design it results in error, I am obviously missing some core concept of OOP.  Can anyone provide some guidance where I am going astray?

class AnimalFactory
{
    public function __construct($mapper) {
        $this->mapper=$mapper;
    }
    public function create($prototype) {
        //bla bla bla script
        return $this->mapper->make($prototype);
    }
}

interface DogFactoryInterface
{
    public function create(DogPrototype $prototype): Dog;
}
interface CatFactoryInterface
{
    public function create(CatPrototype $prototype): Cat;
}

class DogFactory extends Animal implements DogFactoryInterface{}
class CatFactory extends Animal implements CatFactoryInterface{}

class DogPrototype{}
class CatInterface{}
class DogMapper{}
class CatMapper{}

$dogFactory=new DogFactory(new DogMapper());
$dog=$dogFactory->create(new DogPrototype());

 


NotionCommotion

#2 requinix

requinix
  • Administrators
  • Impoverished Administrator
  • 9,874 posts
  • LocationWA

Posted 11 May 2018 - 03:20 PM

It obeys the rules of OOP but not the rules of PHP: return types are invariant - they must be the same. IIRC technical limitations that they do want to remove someday.

So make Dog and Cat extend Animal, if they aren't already, and have Animal as the return type.
"Basically, I think the general rule of thumb is: if someone really wants the blood that's inside of your body, and they're like a vampire, or a dracula, or some sort of man-squito, then that's probably okay. A dracula and a man-squito are made for removing things like blood and swords from inside your body. That's basically fine. If something wants to get at your blood and they're, say, some kind of murdersaurus, or maybe a really big frog, that's where the problems start to arise. A really big frog is not made for removing blood, and your blood knows this, which is why it is so vehement about wanting to stay in your body instead of coming out. Unfortunately this will not deter a really big frog because a really big frog is full of things like prizes, and value, and quite a lot of hatred, and it would really rather like to replace any and all of those things with your blood, and basically by any means possible." --slumbermancer

#3 NotionCommotion

NotionCommotion
  • Members
  • PipPipPip
  • Advanced Member
  • 1,601 posts

Posted 11 May 2018 - 04:59 PM

Thanks requinix,  Don't know if I am happy or said that it is a PHP limitation as the alternative is that it is a me limitation.


NotionCommotion




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users