1

So I have a class:

<?php 
class Database extends PDO implements DatabaseCore
{
  private $connections;
  private $runQueryType = array('select'=>array(),
      'update'=>array(),
      'insert'=>array(),
      'delete'=>array());
  private $activeDB;
  private $databaseKeys = array();

  function __construct()
  {
    global $databases;
    foreach ($databases as $key=>$value) {
      $this->openConnection($value['dsn'], $value['user'], $value['password'], $key);
      array_push($this->databaseKeys, $key);
      $this->enumerateQueryRights($key, $value['rights']);
      Query::initQuery();
    }
  }

  function __destruct()
  {
    foreach ($this->databaseKeys as $key) {
      unset($this->connections->$key);
    }
  }

  function enumerateQueryRights($key, array $rights = array())
  {
    foreach ($rights as $r) {
      array_push($this->runQueryType[$r], $key);
    }
  }

  public function getConnection($key = 'mysqli')
  {
    //ERROR_HERE: PHP says $this is not an object at this point??
    return $this->connections->$key;
  }

  function parseConnectionInfo()
  {

  }

  function getRightByKey($key, $op = 'select')
  {
    return in_array($key, $this->runQueryType[$op]);
  }
  function closeConnection($key)
  {
    if (isset($this->connections->$key)) {
      unset($this->connections->$key);
      return TRUE;
    } else {
      return FALSE;
    }
  }

  function getConnectionInfo($key)
  {

  }

  function getConnectionKeys()
  {
    return array_values($this->databaseKeys);
  }

  function openConnection($dsn, $user, $password, $key = 'mysqli')
  {
    try {
      $this->connections->$key = new PDO($dsn, $user, $password);
      $this->connections->$key->setAttribute(PDO::ATTR_EMULATE_PREPARES, TRUE);
      $this->connections->$key->setAttribute(PDO::ATTR_ERRMODE,     PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
      throw new Exception('Error: connection to Database');
      error_log($key  . '_CONNECT_ERROR' . "\t" . $e);
      set_message('We\'ve encountered an error.  To try again, please log into <a     href="https://powerschool.bps101.net">Powerschool</a>.', 'error');
      mail('[email protected]', 'Error connecting to '.$key, $e);
      die('We can\'t connect to our database, an administrator has been notified');
    }
  }

  public function getError()
  {

  }
}//class

In the function getConnection($key = 'mysqli') I have the return $this->connections-$key;

The error that php throws is right at that point:

Fatal error: Using $this when not in object context in /var/www/html/projects/fees/library/class/database.inc on line 40 (if I counted right, look for the comment in the code, ERROR_HERE)

I'm really not sure why $this is not considered an object at that point in the script. Any advice would be greatly appreciated and if you need more info, please ask.

4
  • 1
    Defining private $connections; as an instance variable, but trying to access it as a static unset(self::$connections->$key); in your destructor is pretty weird Commented Feb 7, 2013 at 16:20
  • 1
    But is it crucial to your business security to hide which line the error is occurring at? Commented Feb 7, 2013 at 16:21
  • I edited that. I was in the middle of converting from static access to true class member with $this and missed that. Commented Feb 7, 2013 at 16:26
  • No, the context for which the line is erroring doesn't make sense I ripped out only the needed code for this post. There are several interfaces and classes in that file that make it not match up. I commented my code with the line at which the error is happening. I suppose I will also count to get the exact line given the context. Commented Feb 7, 2013 at 16:28

1 Answer 1

1

How are you calling getConnection() ? Are you trying to call it as a static method?

eg are you doing something like this:

$conn = Database::getConnection();

If that's how you're calling it, then no, you can't use $this in that context, because it's static; it isn't referencing an object. You'd need the connections property to be static as well.

Sign up to request clarification or add additional context in comments.

3 Comments

Dang! Thank you for asking that question. This will be the answer. My Query class is an abstract class that extends my Database class. However, the Query class does not instantiate my Database class; it is created elsewhere in my script. Because of this, I have coded this in a way that I have to make $connections be static. Otherwise I'd have to have the Query class create an instance of my Database class. Am I right in that thinking?
Yes, that sounds right. I think. It's a good idea to explicitly define any static properies and methods as static.
Yeah, I will go make that method explicitly static. Thank you for helping jump down that path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.