1

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?

9
  • 3
    Your newClass needs to extend Inheritor not Database Commented Oct 15, 2013 at 16:13
  • Will that also allow me to get access to the Database methods?? @ChristopherMorrissey Commented Oct 15, 2013 at 16:14
  • Sounds to me like newClass should extend Inheritor, or that function abcd should be a member of Database. 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. Commented Oct 15, 2013 at 16:14
  • if newClass needs access to methods in Inheritor, why wouldn't newClass extend Inheritor? Or you could move shared functions into Database so both child classes have access to them. Commented Oct 15, 2013 at 16:14
  • 1
    @coderunner that is correct Commented Oct 15, 2013 at 16:27

2 Answers 2

3

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();
       //...
   }
}
Sign up to request clarification or add additional context in comments.

Comments

2

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() 

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.