tags:

views:

1239

answers:

5

I need help in designing my PHP classes where I need to extend from multiple classes.

I have a general class, Pagination.php that does all sort of pagination and sorting. All other classes will use this for pagination.

To make my life easier, I made a class generator that generates a class from MySQL table. All the properties, getters, setters and common methods are created automatically, which really saves time and money.

As an example, class Staff_Base in Staff_Base.php is generated automatically from SQL table t_staff.

Since class Staff_Base is automatically generated from SQL table, any 'custom' methods / properties are located in another class that extends Staff_Base.php. (So that whenever a new field is added, I can simply regenerate Staff_Base class and overwrite in Staff_Base.php).

So I have class Staff.php that extends Staff_Base.php.

The problem is, Staff.php also needs to extend another class, Pagination.php. (The current workaround is to put methods in Pagination.php into every class. This is really troublesome whenever I make changes to the pagination/sorting methods.)

How do I do this? What is the best design pattern to achieve this?

I know common suggestions to restructure my classes, but I really think hard of other workaround/solution. Also, I may also need to extend other classes than Pagination.php.

Thanks!

+1  A: 

you cant, multiple inheritance is not supported in php, but if you do a google search on this topic you can find some workarounds...

A: 

Well, you might already know that PHP doesn't support multiple inheritance. One way around might be using Interfaces instead of superclasses, although, if the logic is identical for each implementing of the interface, this might become tedious. How about writing a code generator, that simply injects the methods to each class? You seem to already do that on the "common methods".

Oh, and using getters and setters (as they are used in e.g. Java) in PHP is considered not a good idea. Objects are slow as they are, so using public fields is considered the norm.

Edit: Then there's the __call()-hack, which could recognize the methods that actually reside in your other classes, and call them manually.

Henrik Paul
+6  A: 

Can you have your generated Staff_Base class inherit from Pagination? Or does Staff_Base already inherit from another base class (that you do not have control over)...

Sounds like either Doctrine or Propel, I do not recall which uses the *_Base class system.

My suggestion would be to rewrite pagination to be able to be used by your entity classes instead of requiring your entity classes to extend it.

Beau Simensen
A: 

I would recommend changing your Staff_Base.php generator to make that class extend Pagination by default. That way Staff extends Staff_Base, and Staff_Base extends Pagination. I think that's probably the cleanest (and most object-oriented) way of getting the results you want.

Ross
+2  A: 

So if I am reading what you wrote correctly, since you can't inherit from 2 classes you are duplicating paginate into every class you have.

Class stacking is a solution. One of the first things I googled.

J.J.
Useful link, thanks.
Jonathan