Skip to content

Instantly share code, notes, and snippets.

@JeroenDeDauw
Created February 1, 2015 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeroenDeDauw/f5d3ce3e184f31f0b776 to your computer and use it in GitHub Desktop.
Save JeroenDeDauw/f5d3ce3e184f31f0b776 to your computer and use it in GitHub Desktop.
<?php
interface Identifiable {
/**
* @return Id
*/
public function getId();
/**
* Needs to be an id of the correct type.
* If the type is wrong, an InvalidArgumentException is thrown.
*
* @param Id $id
*/
public function setId( $id );
}
class Kitten implements Identifiable {
/**
* @param KittenId $id
*/
public function setId( $id ) {
if ( !( $id instanceof KittenId ) ) {
throw new InvalidArgumentException( 'Needs to be a KittenId' );
}
}
}
class Pony implements Identifiable {
/**
* @param PonyId $id
*/
public function setId( $id ) {
if ( !( $id instanceof PonyId ) ) {
throw new InvalidArgumentException( 'Needs to be a PonyId' );
}
}
}
abstract class Id {
public abstract function toString();
}
class KittenId extends Id {
// Makes sure all Kitten ids start with a "k"
}
class PonyId extends Id {
// Makes sure all pony ids start with a "p"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment