views:

235

answers:

2

A very common pattern I see (I'm picking on Zend Framework, only because I was dealing with it at the moment of this question), is something like this:

class My_Form extends Zend_Form {
     public function init() {
          $this->addElement();
     }
}

Zend_Form is not an abstract class, but is perfectly usable on its own. This seems to be "recommended" as place to "encapsulate" your forms into a nice class.

Does this violate the Liskov Substitution Principle? Each subclass of Zend_Form will have a wildy different behavior than the base class. Would it be better to use composition for this, or am I totally misunderstanding this principle?

A: 

Liskov's Substitution Principle states that if a program module is using a Base class, then the reference to the Base class can be replaced with a Derived class without affecting the functionality of the program module.

So how different is MyForm from ZendForm? Would it change the functionality of the program?

Also, check these 2 bullets (from Wikipedia article) :

  • Preconditions cannot be strengthened in a subclass.
  • Postconditions cannot be weakened in a subclass.

Perhaps you will find out that MyForm is not so dramatically different from the ZendForm, and you can safely use inheritance.

Igor Oks
It would change the functionality of the program in that 1) A different HTML form would be rendered, 2) Different rules for validation might be used.
blockhead
Subclasses always do something different from what the base class does, otherwise they wouldn't be needed. So, 1) doesn't contradict the LSP. Maybe 2) does because of (quoting Wikipedia again) "In addition, the principle implies that no new exceptions should be thrown by methods of the subclass, except where those exceptions are themselves subtypes of exceptions thrown by the methods of the superclass."
Igor Oks
The practicalities are such that it makes more sense to extend Zend_Form and I wouldn't be overly by academia
David Caunt
A: 

Zend_Form is designed for inheritance. While using Zend_Form everybody should keep that in mind - in reality it is going to be not necessary Zend_Form but may be its subclass. So, if any program relies on Zend_Form to behave exactly as it does, not as its subclass can behave - that program is wrong. It is not "using Base class", as Liskov principle states, it is abusing it.

Zend_Form is used mostly by Zend framework and I'm sure it uses it correctly.

I think for such classes, designed for inheritance and used as build blocks of application based on some framework, definition of "behavior" should be more abstract - leaving some details to subclass, even if class itself is not abstract. I would say that behavior for Zend_Form is "to render some html and use some rules of validation". In this sense all subclasses of Zend_Form behave in the same way. Being Zend_Form non-abstract just defines default behavior which makes it easier to use.

Also to make it a little bit more academic, I could make two classes from it. One should be abstract - base class for all forms. Another one for empty form, that behaves exactly and Zend_Form behaves now and usable on it's own. So it would be something like

// sorry, I don't like PHP so here goes java 
public abstract class ZendForm{/*implementation here and NO abstract methods*/} 
public final class DefaultZendForm extends ZendForm{/*nothing here*/}

This would remove any confusion about Liskov principle, but ptobably would not add any real value to the program.

Subclass should be different is some way from superclass, otherwise it does not make sense to create subclass. And you always can abuse this difference and write a program that works for superclass and fails for subclass. But it would not be reasonable. Rendering exactly default (empty) form is not a part of Zend_Form's contract. Only behavior that is part of the class contract is a subject for LSP.

Pavel Feldman