Thursday, December 10, 2009

Interfaces in php

implements: (php5.x)

To implement an interface, the implements operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal error. Classes may implement more than one interface if desired by separating each interface with a comma.

Note: A class cannot implement two interfaces that share function names, since it would cause ambiguity.

Note: Interfaces can be extended like classes using the extends operator.

Note: The class implementing the interface must use the exact same method signatures as are defined in the interface. Not doing so will result in a fatal error.

interface a
{
public function foo();
}

interface b extends a
{
public function baz(Baz $baz);
}

// This will work
class c implements b
{
public function foo()
{
}

public function baz(Baz $baz)
{
}
}

No comments:

Post a Comment