Created
September 17, 2013 07:05
-
-
Save andriesss/6590912 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface Comparable { | |
function compare(self $compare); | |
} | |
class Foo implements Comparable { | |
function compare(self $compare) {} | |
} | |
class Bar implements Comparable { | |
function compare(self $compare) {} | |
} | |
$foo = new Foo(); | |
$bar = new Bar(); | |
$foo->compare($foo); | |
$bar->compare($bar); |
@Ocramius, now the method accepts any class that implements the comparible interface. I want to define that a method can only accept an instance of itself.
@andriesss that could work with the static
keyword (unsupported), but is still wrong.
Pretty lame, seems like a nice feature, I guess.
@andriesss the fact is that every implementation basically accepts different types of objects. That's a mess from a LSP pov
@Ocramius, the only option I have is:
<?php
interface Comparable {
function compare(Comparable $compare);
}
class Foo implements Comparable {
function compare(Comparable $compare) {
if (!$compare instanceof self) {
throw new \InvalidArgumentException('sometimes php sucks a little');
}
}
}
class Bar implements Comparable {
function compare(Comparable $compare) {
if (!$compare instanceof self) {
throw new \InvalidArgumentException('sometimes php sucks a little');
}
}
}
$foo = new Foo();
$bar = new Bar();
$foo->compare($foo);
$bar->compare($bar);
@andriesss that's about right... Again, I don't think that's a PHP issue.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@andriess I think the interface works fine:
http://3v4l.org/VJTnU