Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In PHP I have classes as below

class Animal {
    //some vars
    public function printname(){ 
        echo $this->name;
    }
}

class AnimalMySql extends Animal {
    static public function getTableFields(){ 
        return array();
    }
}

class AnimalPostgreSql extends Animal {
    static public function getTableFields(){ 
        return array();
    }
}

Now I have an object $lion = new Animal(); and I want to do

if($store == mysql)
    //getTableFields from class AnimalMySql
else
    //getTableFields form class AnimalPostgreSql

I am new to OOP and not sure what is the best way to call the method from the specific class P.S. Please leave a note with the answer to explain the efficiency of the approach

share|improve this question
1  
Your design is wrong. Your data object should be polymorphic in its data, not in the storage engine. Factor out the database-specific parts into its own class hierarchy. –  tdammers Jun 16 '12 at 10:11
    
@tdammers I don't get it completely, could you give a small pseudo code example –  nischayn22 Jun 16 '12 at 10:24
    
What I mean is that the Animal class should describe an animal, not the mechanism that is used to store it in a database. A lion is a lion, and the same rules apply for it regardless of how you persist it. Don't diversify the Animal class, instead, diversify your Database class and make it so that each subclass (MysqlDatabase, PostgresDatabase) can query and store Animal objects. –  tdammers Jun 16 '12 at 11:16
    
@tdammers we had that initially, then we wanted to separate out the DB access code for Animals and Humans (so that it would be easy to optimize/change them separably) so I thought of AnimalMySql and HumanMySQL and similarly for PgSql.. –  nischayn22 Jun 16 '12 at 11:31
    
Don't go there. Suppose at some point you have not only Animal and Human, but also sixteen other entities. Then suddenly someone decides you have to support SQLite - congratulations, now you have to add sixteen more classes to your system. If you diversify on the Database class, all you have to do is implement SQLiteDatabase, and you're done. –  tdammers Jun 16 '12 at 12:19

1 Answer 1

To call the correct function based on the type data store will look as follow:

if($store == mysql)
{
  $lion = new AnimalMySql(); //extends the Animal class
  $array = $lion ->getTableFields();
  //Alternatively - $array = $lion::getTableFields();

}
else
{
  $lion = new AnimalPostgreSql(); //extends the Animal class
  $array = $lion ->getTableFields();
  //Alternatively - $array = $lion::getTableFields();


}

To make it more effecient, put the getTableFields method in the Animal class seeing that it's function is exactly the same for both sub classes. When you define a subclass to extend to the Animal class the method will be exposed instantly. this is standard method to call classes in PHP. Striving to write fewer lines of code is always a good idea to keep code manageable and more efficient.

share|improve this answer
    
I don't think the getTableFields can be put into Animal class for my project (ofc that would be very good otherwise). Could you elaborate more on "extends the Animal class"? –  nischayn22 Jun 16 '12 at 6:38
    
$lion = new Animal() serves the same purpose as $lion = new AnimalPostgreSql() or $lion = AnimalMySql() because of AnimalPostgreSql/AnimalMySql extends Animal the suggested database classes automatically inherits all functions and methods that are public from the Animal class thereby extends. From what I read from your question I gave the best answer in context. There are however couple more ways to achieve what you want, optimally. If you can provide more information on you what you want to achieve I can assist. –  JLC007 Jun 16 '12 at 19:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.