Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct Implementation of Virtual Functions in PHP?

Tags:

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?

like image 959
erikbstack Avatar asked Aug 28 '09 22:08

erikbstack


People also ask

How do you implement the virtual function?

The virtual functions must be declared in the public section of the class. They cannot be static or friend function also cannot be the virtual function of another class. The virtual functions should be accessed using a pointer to achieve run time polymorphism.

What is a virtual function and how is it implemented?

A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer.

What is virtual function in PHP?

virtual() is an Apache-specific function which is similar to <! --#include virtual...--> in mod_include . It performs an Apache sub-request. It is useful for including CGI scripts or .

Can virtual function have implementation?

Yes, that's fine ... you only need to implement any pure virtual functions in order to instantiate a class derived from an abstract base class.


2 Answers

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.

like image 195
Bob Fanger Avatar answered Sep 21 '22 12:09

Bob Fanger


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.

like image 40
Rob Avatar answered Sep 19 '22 12:09

Rob