You are here: Re: Proper object extension convention « PHP Programming Language « IT news, forums, messages
Re: Proper object extension convention

Posted by Jerry Stuckle on 08/22/06 10:56

terence.parker@gmail.com wrote:
> I'm writing a bunch of classes (specifically, for parsing) but have a
> question about class inheritance.
>
> I understand that with textbook object orientation, a class
> extension/inheritance represents "a type of" ... like a carrot being a
> 'type of vegetable'. However, in my case there is no such association :
> I am making a class for parsing and extending it to another class
> called Alert.
>
> 'Alert' is not a type of 'Parser', but because I want to share some of
> the functions/variables defined in Parser I decided inheritance was the
> best way to achieve this. I don't particularly want to re-instantiate
> Parser inside of Alert, nor do I want to redo things such as MySQL
> connections.
>

If Alert is not a type of Parser, then you should not be deriving Alert
from Parser.

> Someone told me that a solution is to pass in my 'Parser' object to the
> new Alert instance ... but is this better?
>

That's one way.

> Or should I just ignore what the textbook says and do what is easiest
> for me... or is there another method i'm overlooking?
>

The textbook says this for a reason. It's not just theory - it will
eventually cause other problems in your code, also.

> Thanks!
>
> Terence
>

Or, extract the common elements of both into a third class and derive
both Alert and Parser from that class. That is, of course, assuming
they do have something in common. Not seeing your classes, I can't tell.

But this isn't going to help you with common variables. Instantiating a
new Alert class will give you all new variables - even if you derive
Alert from Parser.

Or, maybe you have things in Parser which really don't belong in Parser
- they should be in their own class and passed to Parser. For instance,
the code to connect to a database isn't really important in Parser.
Sure it needs to be done, but it's not critical to that class's
processing. So it should be in its own class. Then you can also use it
in other classes.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация