You are here: Re: downcasting in php « PHP Programming Language « IT news, forums, messages
Re: downcasting in php

Posted by Willem Bogaerts on 05/25/07 06:57

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

 

Navigation:

[Reply to this 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

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