|
Posted by Jerry Stuckle on 09/04/07 11:28
Marijn wrote:
> 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
>
I agree with the others - the first one. Why would you be creating a
DatabaseFactory if you aren't going to create a DatabaseConnection?
Now if you needed to do some things before creating the connection (and
couldn't do them in the constructor for the factory), you need to use
the second version.
But if you don't need this, let the factory create the connection
without unnecessary calls.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|