简体   繁体   中英

PHP classes: Need help to inherit two classes

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!

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.

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 .

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.

你不能在PHP中支持多重继承,但是如果你对这个主题进行谷歌搜索,你可以找到一些解决方法......

It sounds like you're mixing things up here. A class (such as a Staff class) is used to represent a single entity. Eg:

$john = new Staff('John');

How exactly does the paging fit into this? Being page-able (paginatable?) sounds like a property of whatever it is that allows access to these Staff entities, not of the entity itself. That way, the way is clear for each type of Staff class you create to inherit from the base class.

So, what I believe would be the solution you need:

  1. A Staff class (Staff_Base, and its graph of children)
  2. A Staff Data Access Object (DAO\\Staff would be a nice name, if you're using namespaces)
  3. An Interface, to signal to the world that a DAO can be paged

Import to note is that there is no direct inheritance between the DAO class and the Staff class. You can still generate the Staff_Base class based on its properties in the database, and extend from there... as long as you don't include the actual data access in that class.

The code using this would then look something like this:

 <?php
 $staffDao = new DAO\Staff;
 $staffMembers = $staffDao->getPagedResult($start, $amount);
 ?>

Edited to emphasize that the inheritance structure should be separate from the actual retrieval

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 eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM