Skip to content

Instantly share code, notes, and snippets.

@md2perpe
Created November 18, 2012 18:55
Show Gist options
  • Save md2perpe/4106835 to your computer and use it in GitHub Desktop.
Save md2perpe/4106835 to your computer and use it in GitHub Desktop.
Suggestion of alternative "implements interface" declaration

Suppose that two interfaces clash:

interface Alpha
{
	function foo();
}

interface Beta
{
	function foo();
}

Currently you can not have a class implement both of them.

I suggest the following syntax to solve this:

class Omega
{
	implements interface Alpha
	{
		public function foo()
		{
			echo "Alpha::foo()<br>";
		}
	}
	
	implements interface Beta
	{
		public function foo()
		{
			echo "Beta::foo()<br>";
		}
	}
}

How should PHP know which interface to use? The most obvious and probable case is that if an interface is specified for a parameter of a function, then that interface should be used:

function bar(Beta $beta)
{
    $beta->foo();  // prints "Beta::foo()"
}

bar(new Omega());

Here's my suggestion of how to more directly specify which of the two methods to be used:

$omega->Alpha::foo(); // prints "Alpha::foo()"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment