How can I use the PDO class in another class ?

Example I want to use PDO in

class shirt {
	var $id;
    var $filename;
	var $template;
	var $side = 'front';
	var $category = 'shirt';
	var $fonts = array();
	var $images = array();

function shirt() {
    $this->CreateID();
}

function CreateID() {
	$length = 5;

	if (!isset($_SESSION['id'])) { //IF SESSION NOT SET, CREATE RANDOM ID
		$aZ09 = array_merge(range('A', 'Z'), range('a', 'z'),range(0, 9)); 
		$rand =''; 
		for($c=0; $c < $length; $c++) { 
		   $rand .= $aZ09[mt_rand(0,count($aZ09)-1)]; 
		}

		$this->id = strtolower($rand . time());
		$this->PrepareTemplate();

	}
	else {
		$this->id = $_SESSION['id'];
	}
}

function PrepareTemplate() {			
	HERE IT GOES WRONG
	$sql = $db->prepare('SELECT a.id, a.front, a.back, b.title FROM items a, categories b WHERE a.categoryid=b.id LIMIT 1 ASC'); 
	$sql->execute();	
	$preview = $sql->fetchAll();

	$db = null;

	print_r($preview);	
}
}

I have no idea where to create the PDO $db ?

    Usually (but not always) I pass it as a parameter to the constructor, so that I can use the same PDO instance throughout the script.

    class shirt {
        var $db; // PDO object
        var $id;
        var $filename;
        var $template;
        var $side = 'front';
        var $category = 'shirt';
        var $fonts = array();
        var $images = array();
    
    function shirt($db) { // use type-hinting if only need to support PHP5: shirt(PDO $db)
        $this->db = $db;
        $this->CreateID();
    }
    }
    
    // usage:
    $pdo = new PDO($dsn);
    $shirt = new shirt($pdo);
    

      Oke thanks for you answer, I can work that out (probably).

      But you pass it in every class to the constructor (if needed there).?
      Also this won't flood the database with open connections ?

        Terminator;10961341 wrote:

        Oke thanks for you answer, I can work that out (probably).

        But you pass it in every class to the constructor (if needed there).?
        Also this won't flood the database with open connections ?

        Yes, I would pass it to each class that needs it. No, it would not "flood" the DB, as its the same object being passed to each new object that needs it, so they're all using the same connection (as opposed to each object instantiating its own PDO object, which would create a new connection for each such instance). It also means that you can keep the DB connection info separate and in one place, instead of having it in each class, which is both easier to maintain and keeps your classes focused on one cohesive area of data/functionality.

        FYI, there is an alternative which some people like, which would be to extend PDO into a singleton pattern class, and then instantiate that from within each class that needs it. In that case, the connection parameters could be in that singleton class*, but the singleton has its own baggage of potential issues, so I prefer the good old constructor parameter technique.


        • You still might want to make a child class of PDO for this or other reasons, in which case you could pass that as the constructor parameter instead of the base PDO object.
          Write a Reply...