Can an interface class extend another interface class in PHP

Yes - it can in PHP version 5.2.
(The example code below is also available here.)

interface testinterfaceA
	{
	public function method1($arg);
	}

interface testinterfaceB extends testinterfaceA
	{
	public function method2($arg);
	}

class concrete implements testinterfaceB
	{
	public function method1($arg)
		{
		//do something
		}

	public function method2($arg)
		{
		//do something
		}
	}

The code works.

One thing to note.

An interface declares ‘public’ methods by its very nature (it specifies how an implementing class looks from the perspective of clients).

Therefore, in PHP 5.2, you  cannot have an interface method declared as ‘protected’ or ‘private’. Otherwise you will get a fatal error along the lines of:

Fatal error: Access type for interface method MyInterfaceClass::MyWronglyProtectedMethod() must be omitted in /path/to/your/script.php on line xx

See: http://bugs.php.net/bug.php?id=28158

I write these notes at the risk of someone saying “That’s bloody obvious”. However, I think that these things are not necessarily ‘obvious’. Rather, they are ‘implicit’ within OOP principals. Hence, there is no harm in stating them explicitly.

Leave a Reply