Skip to content

Instantly share code, notes, and snippets.

@andriesss
Created September 17, 2013 07:05
Show Gist options
  • Save andriesss/6590912 to your computer and use it in GitHub Desktop.
Save andriesss/6590912 to your computer and use it in GitHub Desktop.
<?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);
@andriesss
Copy link
Author

@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.

@Ocramius
Copy link

@andriesss that could work with the static keyword (unsupported), but is still wrong.

@andriesss
Copy link
Author

Pretty lame, seems like a nice feature, I guess.

@Ocramius
Copy link

@andriesss the fact is that every implementation basically accepts different types of objects. That's a mess from a LSP pov

@andriesss
Copy link
Author

@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);

@Ocramius
Copy link

@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