Reply to Re: Casting Objects Within Class?

Your name:

Reply:


Posted by petethebloke@googlemail.com on 10/03/07 13:03

On 3 Oct, 12:55, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> petethebl...@googlemail.com wrote:
> > I'm not sure how to ask for what I want, so I can't find it on Google.
>
> > I'll explain.....
>
> > I have a class 'hanging' with a constructor:
>
> > class hanging{
> > function hanging(){
> > }
>
> > }
>
> > //and a class curtain
> > class curtain extends hanging{
> > function curtain(){
> > }
>
> > }
>
> > //and a blind
> > class blind extends hanging{
> > function blind(){
> > }
>
> > }
>
> > In the hanging constructor the object gets database info part of which
> > has an ID number for its type, so I want the constructor to have
> > something like....
>
> > function hanging(){
> > //blah blah
> > if ( type_ID is curtain ){
> > $this-> is a curtain object
> > }
> > //etc etc
>
> > }
>
> > But I don't know how. Any help would be appreciated.
>
> > Thanks
>
> > Pete
>
> Pete,
>
> You can't that way. When you create the class, you need to know what
> type it is then - 'curtain' or 'blind', for instance. But once you're
> in the constructor, you already have the object created.
>
> You could create a static method in the hanging class which will
> retrieve the information from the database, then depending on the type,
> create the correct object and return its address. This isn't an ideal
> solution because if you add 'tapestry' as a derived class of hanging,
> you need to go back and modify the static function. But it works.
>
> But I also have to ask - do you really need all three classes? What are
> the differences between 'blind' and 'curtain'? Could they actually be
> handled by a single class?
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Thanks for that Jerry.
I was trying to keep things simple (!).
At the moment I have 'design' and it does everything, but it has 30
odd methods
and I wanted to see if I could make things easier to manage. The idea
was to have
inheriting classes 'cushion', 'blind', 'pelmet', 'curtain' and so on
(it's a soft
furnishings site) where functions relevant only to cushions (or
whatever) would be
in the child class.

The reason I need to call design without knowing its type is that it
has to speak up
for itself in the shopping basket page.

Mind you.... it works at the moment, so maybe I'm only making grief
for myself.

Thanks again.

Pete

[Back to original 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

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