473,322 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Casting Objects Within Class?

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

Oct 3 '07 #1
5 2638
pe**********@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.
js*******@attglobal.net
==================
Oct 3 '07 #2
On 3 Oct, 12:55, Jerry Stuckle <jstuck...@attglobal.netwrote:
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

Oct 3 '07 #3
pe**********@googlemail.com wrote:
On 3 Oct, 12:55, Jerry Stuckle <jstuck...@attglobal.netwrote:
>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.
js*******@attglobal.net
==================
Oct 3 '07 #4
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.
jstuck...@attglobal.net
==================
Pete,

I was wrestling with much the same issue recently with a form library
I'm developing. I wanted there to be a Form class which would be
responsible for managing all the elements in a webpage form and a
FormElement class which the add_element method of the Form class would
use to construct child object elements of specific form field types
(e.g. ElementTextInput, an extension of FormElement).

It doesn't involve a database, but I did want to validate element
types against a central array. For that I ended up using the idea
Jerry suggested here: a static method in the base element class (I may
move it to the form class before I'm finished) that can be called
statically to retrieve a valid list of element types:

function get_valid_types()
{
$V = array(); // return

// element families
$V['INPUT_TEXT'] = array('input_text', 'input_email', 'input_url',
'input_password');
$V['TEXTAREA'] = array('textarea');

// all elements
$V['TYPE'] = array_merge($V['INPUT_TEXT'],$V['TEXTAREA']);

return $V;
}

So if I add a new subclass (say, a radio-button menu class), I can
just update this method.

Like you, I originally was dreaming of something simpler and more
elegant for the library as a whole. The resulting code ended up being
more complicated and required an additional Factory class to make
everything work as transparently on the API end as I wished. But once
I got that sorted out, implementation is pretty simple:

$TestForm = new CeoForm($debug);
$TestForm->add_element($name='text_input', $type='input_text',
$label='input some text', $is_required=1, $maxlength=255,
$minlength=4);
$TestForm->add_element($name='email_input', $type='input_email',
$label='email address', $is_required=1);
$TestForm->add_element($name='url_input', $type='input_url',
$label='web address', $is_required=0);

if ( $TestForm->is_valid() )
{
// insert results in db or whatever...
}
else
{
$show_form = 1;
}

Anyway, your problem strikes me as the same basic problem. If
interested, you can view the source for this library here:

http://klenwell.googlecode.com/svn/t...ceo/ext/input/

Tom

Oct 3 '07 #5
On 3 Oct, 17:49, klenwell <klenw...@gmail.comwrote:
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.
jstuck...@attglobal.net
==================

Pete,

I was wrestling with much the same issue recently with a form library
I'm developing. I wanted there to be a Form class which would be
responsible for managing all the elements in a webpage form and a
FormElement class which the add_element method of the Form class would
use to construct child object elements of specific form field types
(e.g. ElementTextInput, an extension of FormElement).

It doesn't involve a database, but I did want to validate element
types against a central array. For that I ended up using the idea
Jerry suggested here: a static method in the base element class (I may
move it to the form class before I'm finished) that can be called
statically to retrieve a valid list of element types:

function get_valid_types()
{
$V = array(); // return

// element families
$V['INPUT_TEXT'] = array('input_text', 'input_email', 'input_url',
'input_password');
$V['TEXTAREA'] = array('textarea');

// all elements
$V['TYPE'] = array_merge($V['INPUT_TEXT'],$V['TEXTAREA']);

return $V;

}

So if I add a new subclass (say, a radio-button menu class), I can
just update this method.

Like you, I originally was dreaming of something simpler and more
elegant for the library as a whole. The resulting code ended up being
more complicated and required an additional Factory class to make
everything work as transparently on the API end as I wished. But once
I got that sorted out, implementation is pretty simple:

$TestForm = new CeoForm($debug);
$TestForm->add_element($name='text_input', $type='input_text',
$label='input some text', $is_required=1, $maxlength=255,
$minlength=4);
$TestForm->add_element($name='email_input', $type='input_email',
$label='email address', $is_required=1);
$TestForm->add_element($name='url_input', $type='input_url',
$label='web address', $is_required=0);

if ( $TestForm->is_valid() )
{
// insert results in db or whatever...}

else
{
$show_form = 1;

}

Anyway, your problem strikes me as the same basic problem. If
interested, you can view the source for this library here:

http://klenwell.googlecode.com/svn/t...ceo/ext/input/

Tom
Thanks Jerry. Thanks Tom.
I'm diving off somewhere else at the moment but I'll take a look at
that code when I get back.
I appreciate you both having a look at this for me.

Pete

Oct 3 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
3
by: DJ.precario | last post by:
Hi, I have a vector (myVector) of BaseClass objects. I store in it a DerivedClass object (where DerivedClass inherits from BaseClass). Later I want to get the last element in the vector and...
4
by: Fabrizio | last post by:
Hi I cannot figure why it isn't possible to cast a struct array to an object array. I written a structure like this: public struct Test { private int TestA; private int TestB;
8
by: Razak | last post by:
Hi, I have a class which basically do Impersonation in my web application. From MS KB sample:- ++++++++++++++++++++code starts Dim impersonationContext As...
61
by: Ken Allen | last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years. One of the drawbacks with VB6 and earlier was the difficulty in casting a 'record' to a different 'shape' so one could...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
5
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style...
11
by: Frederic Rentsch | last post by:
Hi all, If I derive a class from another one because I need a few extra features, is there a way to promote the base class to the derived one without having to make copies of all attributes? ...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
9
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.