> 
> **Possible Duplicate:**  
> 
> [What is the best method for getting a database connection/object into a function in PHP?](https://stackoverflow.com/questions/228590/what-is-the-best-method-for-getting-a-database-connection-object-into-a-function)  
> 
> [Database and OOP Practices in PHP](https://stackoverflow.com/questions/1716652/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.