Reply to Re: Code readability vs. execution time

Your name:

Reply:


Posted by ELINTPimp on 09/04/07 13:52

On Sep 4, 2:40 am, Marijn <marijn.huizendv...@gmail.com> 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

>From looking at your code, it looks like you intend to put this
connection function in all of your classes that need to talk to the
database. Your code looks like it wants a singleton pattern.

You're on the right path with your code, but why not abstract that
logic to a class of its own (I assume mysql):

class db {
static private $_instance;
public $connection;

private __construct () {
//do database initiation, such as loading config data and the like
$this->connection = mysql_connect(...);
//exception handling
mysql_select_db($schema, $this->connection);
//exception handling
}

public static function getInstance() {
if (is_null(self::$_instance)) {
self::$_instance = new db();
}
return self::$_instance;
}
} // end db class


now, in your other classes, you simply type:

class someClass {

public function someMethod() {
//just putting this in a generic method, but you could put it in
the constructor, if you wish
$db = db::getInstance();
//and you connection handle is $db->connection
}

A lot less code in your classes, and a bit easier to understand in my
opinion. Also has the advantages of encapsulation to the db
connection so you can do fun things like centralizing exception
handling and/or create some generic functions to handle your query
formats, etc.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация