Jenny
Jenny

Reputation: 1727

How to replace a class in php?

I want to replace a class which is loaded in main project by $addon= new Add_On; the Add_On class has the structure I like but I want to replace all its details. I can comment out the $addon = new Add_on in main project and load my own , but I don't want to modify the main project's code directly. How can I replace this class?

Upvotes: 0

Views: 1734

Answers (3)

Muhit
Muhit

Reputation: 789

Create a new class extending Add_On class:

Class Replace_Add_On extends Add_On {
    // Override any functions or add new ones
}

Upvotes: -1

dddkkk
dddkkk

Reputation: 13

if you can make change to auto_load or spl_auto_load function in your project ,you will replace these classes with your own.

Upvotes: 0

J. Bruni
J. Bruni

Reputation: 20492

First, rename the Add_On class to something else, like Base_Add_On, then append this to the file where it is defined:

class Add_On extends Base_Add_On
{
    // your custom replacements here
}

Upvotes: 2

Related Questions