On 09/04/02, "Kristian Koehntopp" <[EMAIL PROTECTED]> wrote:
> On Mon, Apr 08, 2002 at 08:57:09PM -0400, fabwash wrote:
> > My vote is "java" like: no MI, no aggregation, single inheritence and use of
> > interfaces :)
> 
> Could you please explain how interfaces promote code reuse?
> 
> I am not the perfect java pro, and as I understand it,
> interfaces define a set of instance variables and methods that
> must be there in order to be compliant to an interface, but
> provide to way to import an implementation of such an interface
> into an existing class.

That's right.  There does need to be a way to make this easier.
Delphi allows this:

type
  IFred = interface
    procedure FredMethod;
  end;

  TFredImpl = class(IFred) // TFredImpl implements IFred
    procedure FredMethod;
  end;

  TMyClass = class(IFred)
  private
    FFred:  IFred;  // Hold a reference to the IFred impl
  public
    // And this tells the compiler to delegate IFred methods
    // to FFred member, which can be located/created at runtime 
    property FredImpl: IFred read FFred implements IFred;
  end;

It's a bit cumbersome, but could be represented as something like
this in PHP, if we had interfaces:

class FredImpl implements IFred {
   function FredMethod() {
   }
};

class MyClassStatic implements IFred, extends FredImpl {
};

class MyClassDynamic implements IFred {
   delegate IFred to $fredimpl;

   function MyClass() {
      $this->fredimpl = new FredImpl;
   }
};

An instance of MyClassStatic looks and smells like an instance of
MyClassDynamic to other objects at runtime, but MyClassDynamic
has the added advantage of being able to dynamically locate the
code for it's implementation of the Fred interface.

--Wez.


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to