Page 1 of 1

Inheritance

Posted: Sat Jun 07, 2003 3:12 pm
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.

Posted: Sat Jun 07, 2003 3:55 pm
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.

Posted: Sat Jun 07, 2003 5:13 pm
by McGruff
Thanks nielsene. Eureka!