Page 1 of 1

Child classes and where to call them from

Posted: Tue Apr 24, 2007 9:13 pm
by timclaason
Let me preface by saying that I have no degree in programming, etc. But I am trying to develop my PHP skills, and I hope in the process, I don't dumb down this forum.

One of the things I try to do in my apps is build classes that serve only 1 purpose. So the end result is that I end up creating a bunch of child classes that extend my main class.

The problem I run into is I end up having functions in childclass1 that are needed by childclass2.

This may be a quite general question, but if a function in one child class is needed by a function in another child class, should I reconsider whether that function should be in the child class or the parent class?

Re: Child classes and where to call them from

Posted: Tue Apr 24, 2007 9:35 pm
by Christopher
timclaason wrote:One of the things I try to do in my apps is build classes that serve only 1 purpose. So the end result is that I end up creating a bunch of child classes that extend my main class.?
The thing that caught my eye is that you seem to have only one "main class." If that is the case the stop that! ;)
timclaason wrote:The problem I run into is I end up having functions in childclass1 that are needed by childclass2.?

This may be a quite general question, but if a function in one child class is needed by a function in another child class, should I reconsider whether that function should be in the child class or the parent class?
Yes. If a function is needed by all child classes then you would naturally move it into the parent class. In fact, it is probably better to build classes and then find common function after you have the functionality you want.

Posted: Tue Apr 24, 2007 9:58 pm
by alex.barylski
OOP was introduced primarily to "promote" better code reuse. Some proedural advocates might claim they can do the same - which is partially true.

If you have a child class A derived from BASE and a class B derived from BASE and child class B requires functions in child class A, those functions, if stored in BASE might make sense. So long as both child classes required that function thats what inheritence is for. The problem arises when you introduce child class C which also derives from BASE but does not *ever* need that function. At this point, you would need to refactor and move that function out of the BASE class and into it's own class (ie: HELPER).

PHP doesn't support multiple inheritence (rightly so, some might argue) so if all three classes A, B, C derived from BASE. A & B would need to use HELPER as a composite, rather than a parent.

Code: Select all

Class BASE{
  function Boo(){}
  function Poo(){}
}

Class A extends BASE{
  function Too()
  {
    parent::Boo();
    parent::Poo();
  }
}

class B extends BASE{
  function Coo()
  {
    parent::Boo();
    parent::Poo();
  }
}
The above code works and is proper use of inheritence :)

But consider the third class added which does not need the function Poo() inside the base:

Code: Select all

Class BASE{
  function Boo(){}
  function Poo(){}
}

Class A extends BASE{
  function Too()
  {
    parent::Boo();
    parent::Poo();
  }
}

class B extends BASE{
  function Coo()
  {
    parent::Boo();
    parent::Poo();
  }
}

class C extends BASE{
  function Soo()
  {
    parent::Boo();
    //parent::Poo(); - NOT needed in this derived class so it should factored out
  }
}
See the problem? Your inheriting from a base class several times but one of those classes doesn't need *all* of the functionality, so inheriting doesn't make sense unless you move that behavior outside of the base into a another class...

Edit: That "other" class is what I previous call HELPER and would need to be used inside class A & B. Sorry forgot to clarify that earlier :P

Cheers :)