Correct Implementation of Virtual Functions in PHP?

at my working place (php only) we have a base class for database abstraction. When you want to add a new database table to the base layer, you have to create a subclass of this base class and override some methods to define individual behaviour for using this table. The normal behaviour should stay the same.

Now I have seen many new programmers at our company, who just override the method for the default behaviour. Some are so "nice" to put in all the default behaviour and just add there individual stuff where they like it, others kill themself trying to use the baseclass and their inheritor.

My first thought to solve this problem, was thinking about abstract methods that should be overriden by inheriting classes. But beside other arguments against abstract methods, "abstract" just does not show why the baseclass can't be used by its own and why these function should be overriden.

After some googling around I didn't find a good answer to implementing "real" virtual functions in php (just that there is a virtual function, that nearly kills all hope of a concrete implementation).

So, what would you do with this matter?


In PHP all public and protected functions are "virtual". You can prevent functions from being overriden by prepending the final keyword. (Or by making them private, but this is probably a bad idea).

In the design of the baseclass I would think of behaviors that subclasses would want to affect. I would for example create empty functions like before_update() and after_insert().

function after_insert() {
  // Virtual

Which the baseclass will call when an update/insert event occurs.

Maybe an is_valid() function which always returns true in the baseclass, and use the commentblock to describe what the consequences are when a subclass return false.

Hopefully this would give you some inspiration.

You can always use the "final" keyword to prevent some of the classes functions from being overridden if people are using the class in the wrong way.

It sounds to me like they are unable to acheive certain functionality hence overriding the methods. You may need to take a look at the design of your classes.

Without an example of the implementation of your base class, it's hard to give concrete info. But a few things come to mind:

  1. Database abstraction is complex stuff to begin with. I understand that you want to keep it lean, clean and mean, but I think it's pretty darn difficult. You really have to take a thorough look at the specs of different DB engines to see what parts are general and what parts need specialization. Also; are you sure you don't have DB abstraction mixed up with the Table Data Gateway pattern, as you are talking about adding DB tables by extending the base class?

  2. The methods of your current base class might be doing too much and/or are not general enough to begin with, if the extended classes are bending over backwards too keep it clean. Maybe you should break the base class interface methods up in smaller protected methods that are general enough to be reused in the overriding methods of the extended classes? Or vice versa: maybe you should have hooks to overridable methods in your interface methods.

  3. Following from point 2: What's wrong with having an abstract class with some general implemented methods, and let your vanilla class (your base class) and other classes inherit from that?

  4. Lastly, maybe you should just enforce an interface to be implemented, in stead of extending the base class?

Need Your Help

Reverse engineer an ORM

database orm database-schema

Given a [mysql] database with a given schema, is it possible to generate an ORM interface for it? doesn't matter if its php, python or perl.

Google chart show title as label

javascript php sql google-visualization axis-labels

Working on a line chart in google chart. Having the issue that I cant show the title of the measurement as label. I am further going to get different measurements, right now I can only show one