I’m attempting to create a class for use with multiple databases (don’t tell me there are already classes that do this, I have an assignment to create my own).
I’m attempting to capitalize on this functionality:
<?php
function test($value)
{
print $value;
}
$testF = "test";
$testF('Newt');
?>
But am having some difficulty figuring out the syntax to use in a class with a class wide variable. Here is the code:
<?php
$testLayer = new Test('root', 'a', 'test', 'mysql');
$sql = 'SELECT * FROM tblmedia';
$testLayer->query($sql);
class Test
{
private $dbType = '';
private $connection = '';
private $mysql = array('query' => 'mysql_query',
'getAssoc' => 'mysql_fetch_assoc',
'numRows' => 'mysql_num_rows');
private $postgreSQL = array('query' => 'pg_query',
'getAssoc' => 'pg_fetch_assoc',
'numRows' => 'pg_num_rows');
public function __construct($un, $pw, $db, $dbType = 'mysql', $host = 'localhost')
{
$this->dbType = $dbType;
switch($this->dbType):
case 'mysql':
$this->connection = mysql_connect($host, $un, $pw);
mysql_select_db($db, $this->connection);
break;
case 'postgreSQL':
$conString = 'host='.$host.' user='.$un.' password='.$pw.' dbname='.$db;
$this->connection = pg_connect($conString);
break;
endswitch;
}
public function query($sql)
{
$result = $this->{$$this->dbType}['query']($sql); // [COLOR=Red]<------- here is the issue[/COLOR]
$this->fb($$this->dbType['getAssoc']($result), 'Results');
}
///////////////////////////////////////////////////////
// This method returns array and variable values
// to the screen
//
// View source to see the results
//
// Used internally for debugging
//
private function fb($value, $text = 'Feedback')
{
print '<!-- '.$text.':
';
print_r($value);
print ' -->
';
return;
}
}
?>
I’m attempting to execute a function listed in the associative array utilizing the $$var syntax, but I’m unclear on how this is to be done on a class wide variable. This does not work, $this->{$$this->dbType}‘query’
Anyone know how/if this can be done?
Thanks in advance!