SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Variable variable $$var in a class

    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:
    Code:
    <?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:
    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); // <------- here is the issue
          $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']($sql)

    Anyone know how/if this can be done?

    Thanks in advance!
    John

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like this is the syntax:
    Code:
        public function query($sql)
        {
          $result = $this->{$this->dbType}['query']($sql);
          $this->fb($this->{$this->dbType}['getAssoc']($result), 'Results');
        }
    John


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •