1

I'm trying to pass a PDO connection object from one class to another. But I'm not being very successfull. And I only want to instanciate only one PDO object.

With the help from dqhendricks and awm I managed to get the following solution working:

class Factory {

  function createUser($id = NULL) {
    return new User(Conn::get_conn(), $id);
  }  
  function createApplication($id = NULL) {
    return new User(Conn::get_conn(), $id);
  }
}

class Conn {
  private static $conn = NULL;

  private function __construct() {}

  private static function init() {
      $conf = self::config();
      try { 
        self::$conn = new PDO($conf['dsn'], $conf['user'], $conf['pass']);
      } 
      catch (PDOException $e) { 
        echo $e->getMessage(); 
      }
  }

  public static function get_conn() {
    if (!self::$conn) { self::init(); }
    return self::$conn;
  }

  private static function config($cfg_file = 'sl.config') {
    $config = parse_ini_file('/../'.$cfg_file);
    $conf = array();

    $conf['user']    = $config['db_user']; 
    $conf['pass']    = $config['db_password'];
    $conf['dsn']     = 'mysql:dbname='.$config['db_name'].';host='.$config['db_host'];

    return $conf;
  }  
}

In my UserDAO class, I can now do this:

class UserDAO {
  private $db;
  private $id;

  function UserDAO (&$db, $id) {
    $this->db = &$db;
    $this->id = &$id;
  }  

  public function getRows($sql)
  {   
    $result = $this->db->query($sql);
    $row = $result->fetch(PDO::FETCH_ASSOC);    
    return $row;            
  }

  function getUsers($limit = 10) {
    $sql ="SELECT * FROM sl_store LIMIT $limit";
    return $this->getRows($sql);
  }
}

//My User class
class User extends UserDAO implements iUser {}

// And to test it working:
$user1 = Factory::createUser('5');
$user2 = Factory::createApplication('7');
1
  • you don't get to return whatever you want from a constructor to determine what a new statement creates. a new statement always creates an object out of the named class. Commented Apr 2, 2011 at 1:59

3 Answers 3

2

How about defining an abstract class which gives you the PDO object on request?

E.g.

abstract class Db {

  private static $x = null;

  private static function init() {
    try {
      self::$x = new PDO(...);
    } catch (PDOException $e) {
      ...
    }
  }

  public static function getX() {
    if (!self::$x) self::init();
    return self::$x;
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Your soultion was better. Thanks :)
I was planning on using Dependency Injection, but have I ended up using Singelton pattern ?
Wow, singleton pattern.... didn't even know it had a name! Cheers. :)
1

no need to have your class create an instance of itself if all you want is an instance of a different object back. maybe make a static method in Conn to return an instance of a db connection.

class Conn {

    // prevent new statement
    private __construct() {}

    public static return_pdo() {
        blah
        blah
        blah
        return $db;
    }

    public static config($file) {
        do stuff
    }
}

then call statically

$pdo = Conn::return_pdo();

Comments

0

It's because new Conn() returns $Conn object, not the value from $Conn->Conn() method. Try this:


class Conn{
 function Conn() {
   $db = new PDO($conf['dsn'], $conf['user'], $conf['pass']);
 }
 function get_db() {
     return $this->db;
 }
}

class Factory {
  function createUser($id = NULL) {
    $new_conn = new Conn();
    $db = $new_conn->get_db();
  }
}

Comments

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.