Page 1 of 1
Interfaces and Classes..
Posted: Sat Jul 15, 2006 8:16 pm
by Jenk
I know why we use interfaces, and to an extent believe it is a questionable reason anyway, but today I realised the one reason I was using them is, at least in the current version of php, void..
Previously I used:
Code: Select all
public function __construct (jmt_iView $view, $folder, &$history, $root, $parent)
where jmt_iView is the interface for jmt_View..
after making some changes today, I accidentaly discovered:
Code: Select all
public function __construct (jmt_View $view, $folder, &$history, $root, $parent)
works exactly the same...
So other than creating an 'easy read' list of functions and other details of the class, why do we need interfaces?
Genuine question, as I do know they were invented for a reason, but at the moment I can't see that reason

Posted: Sat Jul 15, 2006 8:26 pm
by sweatje
If you use the interface, then you can have a class which does not descend from jmt_view, but still supports the interface jmt_iView, be allowed.
Posted: Sat Jul 15, 2006 8:54 pm
by Weirdan
... and an object may implement multiple interfaces
Posted: Sun Jul 16, 2006 9:16 am
by Chris Corbyn
An interface can be for more than one class.
Swift has Swift_IConnection which is used by Swift_SMTP_Connection and Swift_Sendmail_Connection. Swift_IPlugin is used by just about whoever wants to write a plugin.
It should be noted that PHP seems to parse interfaces in the same namespace as classes, which is why what you did above worked.
This probably throws an error with something to the effect of "Class foo already exists".
Code: Select all
interface foo
{
//
}
interface foo
{
//
}
Posted: Sun Jul 16, 2006 10:49 am
by sweatje
Weirdan wrote:... and an object may implement multiple interfaces
d11wtq wrote:It should be noted that PHP seems to parse interfaces in the same namespace as classes, which is why what you did above worked.
"Under the hood" and interface is an abstract class with no concrete methods, for which multiple inheritance is allowed. One unfortunate side effect of this implementation is a class can't support two interfaces with the same method defined
Code: Select all
$ php -r 'interface a { function foo(); } interface b { function foo(); } class c implements a,b {};';
Fatal error: Can't inherit abstract function b::foo() (previously declared abstract in a) in Command line code on line 1
Posted: Sun Jul 16, 2006 4:31 pm
by Christopher
sweatje wrote:If you use the interface, then you can have a class which does not descend from jmt_view, but still supports the interface jmt_iView, be allowed.
Jason says it so clearly it is stunning. This is one of the few reasons that aan interface should be used -- and I would guess that it is pretty infrequent. The other reasons I have found are only needed to do tricks to make things like DI work -- so also rare.
It is my impression that Interfaces are currently over-used in PHP. Conversely I think that Abstract classes are currently under-used in PHP and actually have more value to the average PHP programmer than Interfaces.
Posted: Sun Jul 16, 2006 6:03 pm
by sweatje
arborint wrote:
It is my impression that Interfaces are currently over-used in PHP. Conversely I think that Abstract classes are currently under-used in PHP and actually have more value to the average PHP programmer than Interfaces.
I would agree with that assessment as well.
One other thing which annoys me when I am usign interfaces is that I have to explicitly define the functions even if they are already handled by the __call method. This really comes into play with decorators:
Code: Select all
interface iFoo {
function one();
function two();
function three();
}
class aFoo implements iFoo {
function one() { //code
}
function two() {
}
function three() {
}
}
Now wouldn't this be nice to work:
Code: Select all
class FooDecorator imlements iFoo {
protected $foo;
function __construct(iFoo $foo) {
$this->foo = $foo;
}
function __call(($method, $args) {
return call_user_func_array(
array($this->foo, $method),
$args);
}
}
but no, you have to implement each of the interface methods:
Code: Select all
class FooDecorator implements iFoo {
//...
function one() {
$this->foo->one();
}
function two() {
$this->foo->two();
}
function three() {
$this->foo->three();
}
}
