Interfaces and Classes..

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Interfaces and Classes..

Post 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 :)
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

... and an object may implement multiple interfaces
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
{
    //
}
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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();
  }
}


:(
Post Reply