You are here: Re: [PHP] PHP 5 Object Inheritance « PHP « IT news, forums, messages
Re: [PHP] PHP 5 Object Inheritance

Posted by Chris on 10/21/30 11:21

Jochem Maas wrote:

> Chris wrote:
>
>> Hi,
>>
>> I've got a collection of Element classes (about 8 different ones).
>> They are all subclasses of a single parent element. I'm trying to
>> extend their functionality (both the individual classes, and the
>> parent class they inherit).
>>
>> I can extend each Element subclass with it's new specific
>> functionality, but I would also like to add specific functionality to
>> all of the subclasses. Extending the parent element with the new
>> functionality would *seem* to be the way to go, but I can't make it
>> work None of the Subclasses inherit from the extended superclass. Any
>> thoughts? I'd appreciate any ideas.
>
>
>
> BaseElement
> |- SubElement1
> | \- ExtendedSubElement
> |- SubElement2
> |- SubElement3
> |- SubElement4
> |- SubElement5
> |- SubElement6
> |- SubElement7
> |- SubElement8
> \- ExtendedBaseElement
>
> ExtendedSubElement will never be able to inherit from
> ExtendedBaseElement.
> there is no multiple inheritance in php.
>
Yeah, I understand... Is multiple inheritance something that true OOP
languages can do?

> So either change your 'tree':
>
> BaseElement
> \- ExtendedBaseElement
> |- SubElement1
> | \- ExtendedSubElement
> |- SubElement2
> |- SubElement3
> |- SubElement4
> |- SubElement5
> |- SubElement6
> |- SubElement7
> |- SubElement8
>
> ... stick the functionality of ExtendedBaseElement into BaseElement
> and get rid of the ExtendedBaseElement
>
I can't change the tree, because the non-extended elements still need to
be able to function independantly.

> ... or figure out a neat way to use the 'Decorator Pattern'
> (http://www.google.com/search?q=Decorator+Pattern) in order to
> conditionally make extended functionality available in specific
> descendant classes?
>
That seems like it would work, but itdoesn't feel very clean. I'll look
into some more.

> otherwise post some code (cutdown :-) for people to look at.
>
Well, here is my actual tree with all the internals pulled out:

<?php
abstract class CForm_Element {}
class CForm_Datetime extends CForm_Element {}
class CForm_File extends CForm_Element {}
abstract class CForm_StandardElement extends CForm_Element {}
class CForm_Hidden extends CForm_StandardElement {}
class CForm_Checkbox extends CForm_StandardElement {}
class CForm_Radio extends CForm_StandardElement {}
class CForm_Select extends CForm_StandardElement {}
class CForm_Textarea extends CForm_StandardElement {}
class CForm_Text extends CForm_StandardElement {}
class CForm_Password extends CForm_Text {}
class CForm_Email extends CForm_Text {}
class CForm_Timestamp extends CForm_Text {}
abstract class CForm_Number extends CForm_Text {}
class CForm_Float extends CForm_Number {}
class CForm_Integer extends CForm_Number {}
?>

They are the elements of a Form Object, to create the form and validate
the input. The extended classes I'm working on now associate a database
table with the Form object, and will Create, Delete, and Update rows in
a database.

This is my currently anticpated Structure.
<?php
abstract CAdminForm_Element {}
CAdminForm_Boolean {}
CAdminForm_Email {}
CAdminForm_File {}
CAdminForm_Float {}
CAdminForm_Integer {}
CAdminForm_Password {}
CAdminForm_Select {}
CAdminForm_String {}
?>

When I was typing out this structure I realized that some of those have
2 possible objects they'll need to inherit from, which further
complicates things.

An example: I'll want the CAdminForm_Boolean to optionally be Yes/No
Radio buttons, or a Checkbox. Which is handled with two separate Form
Elements.

I'm *this* close to concluding that it would be better to not extend any
of the new AdminForm classes from any of the old, but rather instantiate
a Form Element class for each AdminForm Element, then use __call() to
pass methods through to the instantied, while overriding the necessary
methods in almost the normal way.

Thanks for your time,
Chris

 

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

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