This may sound stupid to all,but I am facing this issue with the static functions in PHP. Still new to OO programming in PHP so need some help.
I have a DB class which handles all the functions for connection and crud operations in my application.I have another class which extends the DB class and uses the methods in it.
class Database(){
function db_connect(){
//body
}
}
/*****The inheritor class*****/
class Inheritor extends Database{
function abcd(){
$this->db_connect(); //This works good
}
}
But now I have to use the function abcd(){} in a other class as it carries out the same task.The new class is this for example which also extends the database class:
class newClass extends Database{
function otherTask(){
//Here I need to call the function abcd();
}
}
I tried making the function abcd() static but then I cannot use the this in the function definition in class Inheritor. I also tried creating the object of database class and but that's not permissible I believe as it gave error.
Can someone suggest me the right way to achieve what I am trying to?
newClassneeds to extendInheritornotDatabaseDatabasemethods?? @ChristopherMorrisseynewClassshouldextend Inheritor, or thatfunction abcdshould be a member ofDatabase. This is about logically grouping your code and class hierarchy, which is hard to give any advice on without knowing the real purpose of each class.