Posted by Marijn on 09/04/07 06:40
Hi there,
Again a question with no simple yes or no answer at least, opinions
will probably be scattered so no unanimous yes or no answer ;)
What would you think is the best of the following to options given
that it is more likely that the databaseConnection is set?
SomeClass {
[.............]
/**
* Sets the connection to the database in use.
*
* @return DatabaseFactory
*/
private function __getDatabaseConnection(){
if (isset($this->__databaseConnection)) {
return($this->__databaseConnection);
} else {
$this->__databaseConnection = new DatabaseFactory();
$this->__databaseConnection->create();
return($this->__databaseConnection);
};
}
}
Or
SomeClass {
[.............]
/**
* Sets the connection to the database in use.
*
* @return DatabaseFactory
*/
private function __getDatabaseConnection(){
if (!$this->__databaseConnection) {
$this->__databaseConnection = new DatabaseFactory();
};
return($this->__databaseConnection);
}
}
The second option definitely has less repetitive code but does that
really make it the better option given the fact that the
databaseConnection property is probably set?
By the way, another small convention question: Should you terminate
control structures with a semicolon?
Thanks
Marijn
Navigation:
[Reply to this message]
|