Re: PHP5 Bug?? extended abstract type enforcement

From: Rob Long (bobalong_at_gmx.net)
Date: 09/24/04


Date: 24 Sep 2004 01:50:30 -0700


<laidbak69@hotmail.com> wrote in message news:<10l6vq7eoliiq6f@corp.supernews.com>...
> > Abstract classes can never be instantiated, only implementations can.
>
> > The basic concept of interfaces/abstract classes is to provide a prototype
> > which can be used to create implementations.
>
> > If your interface contains methods you don't need, than you are using the
> > wrong interface, it's as simple as that. When you encounter the problems
> you
> > have described, it looks like you have chosen the wrong design for your
> > needs.
>
> In case anyone else looks at this, JW's answer to the question was basically
> perfect --
>
> ____________________________________
> Wil Moore III, MCP | Integrations Specialist

Err no, JW's answer was basically wrong!

Probably one of the most common techniques used in object-oriented
design is to partially implement an interface in an abstract class,
then derive from that class and implement any remaining functions.

Have either of you used the Vector or ArrayList classes in Java? Well
you might be interested to know that they both derive from
AbstractList, which partially implements the interface List.

I've put a reduced PHP example of this principle here:

interface IList
{
        function add($item);
        function get($index);
        function size();
        function isEmpty();
}

abstract class AbstractList implements IList
{
        function isEmpty()
        {
                return $this -> size() == 0;
        }
}

class MyList extends AbstractList
{
        private $elements = array();
        
        function get($index)
        {
                return $this -> elements[$index];
        }

        function add($item)
        {
                array_push($this -> elements, $item);
        }

        function size()
        {
                return count($this -> elements);
        }
}

Now what's wrong with this design? Mmm, nothing. AbstractList provides
a skeleton implementation from which I can derive to create my own
lists. This makes my job as a programmer easier. The same thing
applies to basic collections, from which lists are derived.

It would be nice if I could declare in AbstractList the functions I
intend to implement in subclasses as abstract, as is done in Java.
However, if I attempt to do that PHP tells me that I cannot re-define
a function. This is a little annoying, and the feature should be
included in future versions in order to make code more readable, but I
can live with it.

What's wrong with PHP's implementation of type checking? Well,
precisely what I said before: if I fail to implement any of the
functions get(), add() or size() in MyList, then no compile error is
thrown, as type checking on interfaces does not extend into child
classes.

Try the same thing in Java, see what happens.

Is this good for programmers? No, as it forces you to check your types
manually.

PHP knows something is awry when I try this:

$list = new MyList();

throwing an error telling me I cannot instantiate abstract classes,
which is what I'd expect, having not implemented all the functions in
IList. However I shouldn't have to wait until runtime to find this
out.

Everything clear?

Does anyone else have any more sensible comments on the above
behaviour?



Relevant Pages

  • Re: Design decision for a game
    ... Especially if the Rule implementations all share a common implementation, where the actual listener list is maintained. ... Alternatively, if there are to be different listener interfaces for each Rule, then obviously each Rule instance would have different listener interfaces subscribed. ... Depending on what notifications are required and whether there's a good way to generalize them so that a single RuleListener interface can apply to all Rule implementations, this may also argue in favor of per-Rule listener lists. ...
    (comp.lang.java.programmer)
  • Re: PHP5 Bug?? extended abstract type enforcement
    ... > however get an error when I attempt to instantiate MyExtendedClass: ... Abstract classes can never be instantiated, only implementations can. ... If your interface contains methods you don't need, ...
    (comp.lang.php)
  • Re: PHP5 Bug?? extended abstract type enforcement
    ... > Abstract classes can never be instantiated, only implementations can. ... > wrong interface, ... Wil Moore III, MCP | Integrations Specialist ...
    (comp.lang.php)
  • Re: Features that can only be provided by the implementation?
    ... specific behaviors of standard CL functions allow but not mandated ... such as an interface to sockets.) ... Built-in Functions ... Getopt Parameter lists ...
    (comp.lang.lisp)
  • Re: Visitor pattern, code generation and additional sub classes
    ... this is a base class / interface. ...     void visitFoo; ... in all my visitor implementations ... ...
    (comp.object)