Inheritance

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Inheritance

Post by McGruff »

Manual says php does not support multiple inheritance - each child can only have a single base class.

However, when I tried some tests, I found that with:

parent class A --> child B extends A --> child C extends B

.. child C CAN access the methods and properties of A :)

I guess they mean that you can't have several parents and one child which extends them all.

Anyway, I seem to have discovered that you can (apparently) build "trees" of classes - similar to the filesystem directory tree. I'm looking at using this ability to share out common code efficiently between a bunch of related tasks - for example file writing / folder browsing / ftp.

There would be a single base class with methods common to all branches, maybe an intermediate layer with stuff common to a particular branch, and then a final layer which by now is getting much less abstract: ie childs which draw particular pages for a specific site.

Re-coding the program for a new site would simply mean re-coding the top-level childs.

So, my question is have I got all that right? Before I start re-working thousands of lines of code it would be nice to know that the principle is sound.

This seemed more at home here - but stick it in the normal forum if you think it's more a of a basic query.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Yes multiple inheiritence only refers to the ability to do something like

Code: Select all

class a extends b,c
Where a inhierits directly from both b and c and the languange then needs to deal with what happens when two parent classes provide the same attributes/methods.

Trees of classes are extremely common. In fact if you aren't using them, then you probably aren't using the real "power" of OOP.

As a very simple example, I use a few familes of classes. One is my DB class, which gets subclassed for each functional area of my application that wraps the common queries. Thereby letting the rest of the application view the DB as an "regular" object. No SQL appears outside of the DB class family. Similarly I have a UI base class that ouput handles the outputting of almost all XHTML. Again it gets subclassed to provide more detailed views, but UI provides all my "wrappers" around tables and form elements as needed.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Thanks nielsene. Eureka!
Post Reply