By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

downcasting in php

P: n/a
Hello,

An API function gives me an object of class 'Blah'. I have subclassed
'Blah' and added a few methods:

class EnhancedBlah extends Blah {
function enhancement1() {}
function enhancement2() {}
}

Is there a way to 'downcast' the object to a subclass? Something like
this:

$blah = SuperApi::getBlah();
$enhancedBlah = (EnhancedBlah) $blah;

Thanks for your help.

~RvR

May 25 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
An API function gives me an object of class 'Blah'. I have subclassed
'Blah' and added a few methods:

class EnhancedBlah extends Blah {
function enhancement1() {}
function enhancement2() {}
}

Is there a way to 'downcast' the object to a subclass? Something like
this:

$blah = SuperApi::getBlah();
$enhancedBlah = (EnhancedBlah) $blah;
No. That would break the substitution principle. It is the other way
around. You can create an instance of the subclass and treat it like you
would treat an instance of the superclass. Subclasses are more detailed
than superclasses, so you would need extra info when downcasting. In
fact, superclasses are usually generic terms, and therefore abstract
(non-technical meaning here), even if they don't have abstract methods
in the technical sense.

An everyday example:
The superclass "policeman" has subclasses "inspector", "traffic
policeman", etc. The common functionality gets in the superclass. "make
arrest", for example. You can ask any member of a specific subclass to
perform his duty as a member of the superclass. But not the other way
around. In fact, there is no such thing as a "policeman" instance: every
policeman is a specific member of a subclass of "policeman".

Best regards.
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
May 25 '07 #2

P: n/a
At Thu, 24 May 2007 23:07:18 -0700, Ryan van Roode let his monkeys type:
Hello,

An API function gives me an object of class 'Blah'. I have subclassed
'Blah' and added a few methods:

class EnhancedBlah extends Blah {
function enhancement1() {}
function enhancement2() {}
}

Is there a way to 'downcast' the object to a subclass? Something like
this:

$blah = SuperApi::getBlah();
$enhancedBlah = (EnhancedBlah) $blah;

Thanks for your help.

~RvR
Nope. Simple reason: Every cat (ExtendedBlah) is a mammal (Blah), not
every mammal (Blah) a cat (ExtendedBlah).
So you can treat Enhancedblah as a Blah, but not the other way around.

HTH
Sh.
May 25 '07 #3

P: n/a
On 25.05.2007 08:07 Ryan van Roode wrote:
Hello,

An API function gives me an object of class 'Blah'. I have subclassed
'Blah' and added a few methods:

class EnhancedBlah extends Blah {
function enhancement1() {}
function enhancement2() {}
}

Is there a way to 'downcast' the object to a subclass? Something like
this:

$blah = SuperApi::getBlah();
$enhancedBlah = (EnhancedBlah) $blah;

Thanks for your help.

~RvR
Technically that's possible, see e.g. http://www.tagarga.com/blok/on/061130

but as others already said, the very idea is wrong.
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 25 '07 #4

P: n/a
At Fri, 25 May 2007 10:18:48 +0200, gosha bine let his monkeys type:
On 25.05.2007 08:07 Ryan van Roode wrote:
>Hello,

An API function gives me an object of class 'Blah'. I have subclassed
'Blah' and added a few methods:

class EnhancedBlah extends Blah {
function enhancement1() {}
function enhancement2() {}
}

Is there a way to 'downcast' the object to a subclass? Something like
this:

$blah = SuperApi::getBlah();
$enhancedBlah = (EnhancedBlah) $blah;

Thanks for your help.

~RvR

Technically that's possible, see e.g. http://www.tagarga.com/blok/on/061130

but as others already said, the very idea is wrong.
The effect may be comparable to a sort of cast, but it isn't. It's simply
(ab)using the serialize function to copy just the vars/values and not the
methods of another class.

If OP has a need to do such a thing he probably should loook at his class
design compared to what he was trying to achieve at a more abstract level.
Good chance his original design is flawed.

Sh.
Sh.

May 25 '07 #5

P: n/a
The trick I usually use if I need to do something similar to that is
to create a constructor for the subclass that takes the parent class
as its only parameter. Then you just do

$obj = new Blah();
$obj = new EnhancedBlah($obj);

Same type of effect and you can still use the $obj anywhere you have
before because its a subclass now.

On May 25, 2:07 am, Ryan van Roode <rvanro...@gmail.comwrote:
Hello,

An API function gives me an object of class 'Blah'. I have subclassed
'Blah' and added a few methods:

class EnhancedBlah extends Blah {
function enhancement1() {}
function enhancement2() {}
}

Is there a way to 'downcast' the object to a subclass? Something like
this:

$blah = SuperApi::getBlah();
$enhancedBlah = (EnhancedBlah) $blah;

Thanks for your help.

~RvR

May 25 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.