Skip to content

Instantly share code, notes, and snippets.

@tpavlek
Created November 27, 2015 13:49
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 tpavlek/8e82e9b94aa4d99465f6 to your computer and use it in GitHub Desktop.
Save tpavlek/8e82e9b94aa4d99465f6 to your computer and use it in GitHub Desktop.
Value of late-static typehints
class AbstractValueObject {
public static funtion sum(static ...$values) {
$total = 0;
foreach ($values as $value) {
$total += $value->getIntegerRepresentation();
}
return new static($total);
}
}
class OtherValueObject extends AbstractValueObject {
}
class ConcreteValueObject extends AbstractValueObject {
}
$values = [ new ConcreteValueObject(), new OtherValueObject(), new ConcreteValueObject() ];
// We want this to throw an error, because it's trying to sum one instance of OtherValueObject which isn't the same type as the
// static call.
$total = ConcreteValueObject::sum($values);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment