Reply to Re: Casting Objects Within Class?

Your name:

Reply:


Posted by Jerry Stuckle on 10/03/07 15:13

petethebloke@googlemail.com wrote:
> 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 (!).

Yea, but it's not always that way, unfortunately.

> 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.
>

OK, that's reasonable enough.

> 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
>

I suspect everything's in a single class right now, and some methods are
applicable to one object while not another. This would be a great use
of inheritance.

What I might recommend is to use the static function idea in the
'hanging' class. Maybe pass it a database row id (or the contents of
the row, if you wish) and have it build the appropriate class from the data.

As I said - it's not perfectly clean, but if you want to use
inheritance, someone needs to know what the classes you're using are.
And keeping everything in this one function means you can easily add
more classes to the inheritance hierarchy later.


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

[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

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