Reputation: 935
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?
Upvotes: 1
Views: 95
Reputation: 31
When you extends a class the new class inherit the previous methods. Example:
Class database{
Method a(){}
Method b(){}
Method c(){}
}
Class inheritor extends database{
//this class inherit the previous methods
Method d(){}
}
Class newCalss extends inheritor{
//this class will inherit all previous methods
//if this class you extends the database class you will not have
//the methods d()
}
Upvotes: 2
Reputation: 1696
You can simply extend the Inheritor
class. This will give you access to both Database
and Inheritor
methods.
class NewClass extends Inheritor {
function otherTask() {
//...
$this->abcd();
//...
}
}
Upvotes: 3