Session Objects, Dynamic Parameters
| From: | Joseph Crawford | Date: | Tue, 05 Oct 2004 00:37:53 +0000 |
| Subject: | Session Objects, Dynamic Parameters | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-36598@lists.php.net to get a copy of this message | ||
Guys can anyone explain why this is returning odd results?
function &getObject($class, $params=NULL) {
if (!is_array($_SESSION['objects'])) {
$_SESSION['objects'] = array();
}
if (!isset($_SESSION['objects'][$class])) {
if (is_array($params)) {
// this is what is erroring
$args = implode(", ", array_map("paramType", $params));
echo $class.'('.$args.')';
$_SESSION['objects'][$class] =& new $class($args);
} elseif($params != NULL) {
$_SESSION['objects'][$class] =& new $class(paramType($params));
} else {
$_SESSION['objects'][$class] =& new $class;
}
}
return $_SESSION['objects'][$class];
}
function paramType($var) {
if (is_string($var)) {
return "'".$var."'";
} else {
return $var;
}
}
when i call this
$dbParams = array(
'server' => 'localhost',
'database' => 'ipro2',
'username' => 'root',
'password' => '**********'
);
$db = &getObject('Mysql', $dbParams);
$db->connect();
it seems to take this, (which is the actual call in the getObject function
Mysql('localhost', 'database', 'user', '**********')
it seems that
'localhost', 'database', 'user', '**********'
is being taken as one parameter, i am guessing because the getObject
function creates it as one string, my question is how can i get this
to think it is different arguments, i keep getting errors like this
mysql error: Unknown MySQL Server Host ''localhost', 'database',
'user', '**********'' (-1)
--
Joseph Crawford Jr.
Codebowl Solutions
codebowl@gmail.com
802-558-5247
For a GMail account
contact me OFF-LIST