Wednesday, January 13, 2010

2 Interfaces cannot contain the same method names

Impotrant: 2 Interfaces cannot contain the same method name and be
implemented in the same class

For example:

interface LampcmsResourceInterface
{
/**
* Returnes id of user (USERS.id)
* who owns the resource
*
* @return int
*/
public function getOwnerId();

public function getResourceId();

}


and then a
interface Zend_Acl_Resource_Interface
{
/**
* Returns the string identifier of the Resource
*
* @return string
*/
public function getResourceId();
}

This is not a problem as long as you don't try to implement
both in the same class.

You may assume that if your class has a method

getResourceId() then you can safely say it implements both LampcmsResourceInterface and Zend_Acl_Resource_Interface
but this is not true

if you try to write a class like this

class MyResource implements LampcmsResourceInterface, Zend_Acl_Resource_Interface
{

protected $resourceID = 100;

public function getResourceId(){

return $this->resourceID;
}



}

You will get fatal error when you try to instantiate this class:

Fatal error: Can't inherit abstract function Zend_Acl_Resource_Interface::getResourceId() (previously declared abstract in LampcmsResourceInterface)

The best course of action now would be to
change the LampcmsResourceInterface to only have one required method getOwnerId();

Then if you need an object that MUST have getOwnerID() and getResourceId()

just make a class and declare it as implements LampcmsResourceInterface, Zend_Acl_Resource_Interface

1 comment:

  1. You are wrong, as we can have two interface with the same methods name and implement both interface in a class.I have checked it.

    ReplyDelete