Possible Duplicate:
What is the best method for getting a database connection/object into a function in PHP?
Database and OOP Practices in PHP
I am trying to build an OOP shopping cart.
At present, it is half OOP, and half procedural... e.g.
function removeFromCart() {
require_once('/.../.../connectPDO.php');
$db = connectPDO();
$sql = 'DELETE FROM Quotes WHERE User = :user and ProductId = :pid';
$stmt = $db->prepare($sql);
$stmt->execute(array(':user' => $user, ':pid' => $pid));
}
My problem is that if I wish to add to cart, then in my function addToCart, I will need to require the db connection again.
This seems like a complete waste, considering every function will need to contain the following:
require_once('/.../.../connectPDO.php');
$db = connectPDO();
I am aware that this is completely in-efficient, and was wondering if anybody could help me write a skeleton OOP cart class which uses the above connection to connect to the DB?
Does this go in the constructor??? Will this stay alive when a user navigates from one page to another at the front end?
I am new to OOP and am completely lost.
Many thanks in advance.