|
Posted by Jerry Stuckle on 10/03/07 11:55
petethebloke@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.
jstucklex@attglobal.net
==================
[Back to original message]
|