Page MenuHomePhabricator
Paste P7112

Interface versioning example for T193613
ActivePublic

Authored by Tgr on May 9 2018, 10:12 PM.
Tags
None
Referenced Files
F18105684: Interface change example for T193613
May 9 2018, 10:12 PM
Subscribers
interface Foo {
function frob($x); // should become frob($x, $y = '')
}
class MyFoo implements Foo { // in an extension
function frob($x) { // must not break
...
}
}
class Xyzzy {
function sizzle( Foo $foo ) {
$foo->frob( "an X" ); // wants to call $foo->frob( "an X", "some Y" );
}
}
class Whammo {
function drizzle( Foo $foo ) {
$foo->frob( "another x" ); // should still work
}
}
------------------------------------------------------------------------------------------------
// @deprecated use Foo2 instead
interface Foo {
// Render a dot $x from the left ($y is always 0, not configurable)
function frobDot($x); // @deprecated use Foo2::foo2 instead!
}
interface Foo2 extends Foo {
// Render a dot at $x/$y coord
// Document that some implementations may not support $y !== 0
function frobDotAt($x, $y = 0);
}
class MyFoo implements Foo2 { // in an extension
function frobDot($x) { // implement old interface based on new interface
$this->frobDotAt($x, 0);
}
function frobDotAt($x, $y = 0) {
...
}
}
class Foo2Adapter implements Foo2 {
private $foo;
public funtion __construct( Foo $foo ) {
$this->foo = $foo;
}
function frobDot($x, $y = 0) { // implement new interface based on old interface
if ( $y !== 0) throw ooops! // make sure we can ignore the new parameter
$this->foo->frobDotAt($x); // ignore $y ?
}
}
class Xyzzy {
function sizzle( Foo $foo ) { // wants to hint against Foo2 eventually
$foo2 = Foo2Adapter::newFromFoo( $foo ); // this mapping get's pushed up to callers more and more
$foo2->from2( "an X", "some Y" );
}
}
// stop calling Foo::frob ///////////////////////////////
class Xyzzy {
function sizzle( Foo2 $foo ) { // callers must now do the Foo2Adapter thing
$foo->frob2( "an X", "some Y" );
}
}
class Whammo {
function drizzle( Foo2 $foo ) {
$foo->frob2( "another x", "more y" ); // should still work
}
}
// drop Foo ///////////////////////////////
interface Foo2 {
// Render a dot at $x/$y coord
function frobDotAt($x, $y = 0);
}
// remove B/C methods
class MyFoo implements Foo2 { // in an extension
function frobDotAt($x, $y = 0) {
...
}
}

Event Timeline

Tgr changed the title of this paste from Interface change example for T193613 to Interface versioning example for T193613.May 10 2018, 7:02 AM