I am creating a database connection class as an abstract in the super-class. Am using PDO in php. I want to make it abstract so that i can declare specific database connection sub-classes for mysql, oracle, postgre, etc.
I have a connection method in the abstract super-class that i have also declared as abstract. My problem now is, can i make this connection method also static in the sub-class? I want to know if it is the right thing to do.
EDIT
//SUPER CLASS
abstract protected function connectToDatabase($hostname, $dbName, $userName, $password="");
// SUB CLASS
public static function connectToDatabase($hostname, $dbName, $userName, $password=""){
$this->setHostName($hostname);
$this->setDbName($dbName);
$this->setUserName($userName);
$this->setPassword($password);
$this->setDatabaseType(DATABASE_TYPE);
$dsn = $this->getDatabaseType(DATABASE_TYPE) . ":" . parent::getHostConst() . "="
. $this->getHostName() . ";" . parent::getDbNameConst() . "=" . $this->getDbName();
$pdo = new PDO($dsn, $username, $passwd);
$this->setPdoConnection($pdo);
}
From looking at your code i would say you can’t make them static as you are then refering to $this in them. What should $this mean in a static method?
A static method only exists once in the class and not does not refer to a special object.
For example take a look at: When to use self over $this?