Le 2012-07-03 10:44, Rouslan Placella a écrit :
On 03/07/12 15:42, Marc Delisle wrote:
Le 2012-07-03 10:11, Rouslan Placella a écrit :
On 03/07/12 15:05, Marc Delisle wrote:
Le 2012-07-03 10:00, Rouslan Placella a écrit :
On 03/07/12 14:51, Marc Delisle wrote:
Le 2012-07-03 09:38, Rouslan Placella a écrit : > On 03/07/12 14:34, Marc Delisle wrote: >> Hi, >> I just made a suggestion to Thilina but I want to validate it with >> everyone. >> >> I see a lot of >> $common_functions = PMA_CommonFunctions::getInstance() >> >> inside many functions of the same library. If we know that this library >> is always called from one script, I believe it would be more efficient >> to do this instead, inside the functions: >> >> global $common_functions; >> >> and ensure that $common_functions has been defined in the global scope >> of the calling script. >> > > Doesn't that defeat the purpose of having a singleton class?
Well, I don't understand the definition of a singleton. From [0], "the singleton pattern is a design pattern that restricts the instantiation of a class to one object"
Does that mean that one such object must exist inside a function, or must exist considering all scopes?
There will be only one in all scopes. However I thought that we were trying to reduce the number of globals that we have, since sometimes we get problems with undefined globals. Also, if we have a global reference to the commonFunctions object, then I see no point in restricting the number of instances.
Ok, so what do you think of this code snippet:
$query[] = $insert_command . 'INTO ' . PMA_CommonFunctions::getInstance()->backquote($GLOBALS['db']) . '.' . PMA_CommonFunctions::getInstance()->backquote($GLOBALS['table']) . ' (' . implode(', ', $query_fields) . ') VALUES (' . implode('), (', $value_sets) . ')';
Does this follow the singleton principle?
I don't know about following the principle, but it looks ugly. There should definitely be a local reference to the commonFunctions object.
$commonFunctions = PMA_CommonFunctions::getInstance(); $query[] = $insert_command . 'INTO ' . $commonFunctions->backquote($GLOBALS['db']) . '.' . $commonFunctions->backquote($GLOBALS['table']) . ' (' . implode(', ', $query_fields) . ') VALUES (' . implode('), (', $value_sets) . ')';
I agree; however, in a previous reply you said "There will be only one in all scopes" so I'm confused. Setting $commonFunctions this way in each function, has for effect that multiple instances of this kind of object will exist in all scopes.
The will all be references to the same object. Like pointers in C or C++.
Ok, now I get it. So, let's forget about my suggestion of having a global variable.