|
Posted by Jerry Stuckle on 09/04/07 14:52
ELINTPimp wrote:
> 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.
>
>
I do something very similar in my classes, and find it easier to use.
However, there is a whole design concept around the Factory class, and
some people prefer it. It helps when you need to pass parameters to the
Factory, for instance, before actually creating the object.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|