Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9053593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:27:47+00:00 2026-06-16T13:27:47+00:00

Possible Duplicate: What is the best method for getting a database connection/object into a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:27:48+00:00Added an answer on June 16, 2026 at 1:27 pm

    Something like the following should get you started:

    $pdo = new PDO('your dsn');
    $cartData = new CartData($pdo);
    $cart = new Cart($cartData);
    
    class CartData
    {
        private $dbConnection;
    
        public function __construct(PDO $dbConnection)
        {
            $this->dbConnection = $dbConnection;
        }
    
        public function removeItem($userId, $productId) { 
            $sql = 'DELETE FROM Quotes WHERE User = :user and ProductId = :pid';
    
            $stmt = $this->dbConnection->prepare($sql);
            $stmt->execute(array(':user' => $userId, ':pid' => $productId));
        }
    }
    
    class Cart
    {
        private $cartData;
    
        public function __construct(CartData $cartData)
        {
            $this->cartData = $cartData;
        }
    
        public function removeItem($userId, $productId) { 
            $this->cartData->removeItem($userId, $productId);
        }
    }
    

    Note that I have removed the database calls from the actual Cart class, because that would only make it hard / impossible to swap to another database engine at some point. Or perhaps you might want to introduce a complete other way of storing your data (ahum unit testing). Also note that I have used dependency injection to give the classes the objects they need to be able to perform whatever it is they are responsible for.

    I have used type hinting for the class objects which are being injected, however it would have been better to type hint against an interface, because that would make it easy to swap out the classes for other classes. And I strongly suggest you use interfaces (what I wrote above is simply an example to get the idea). This also make it pretty easy to created unit tests for your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What is the best method to merge two PHP objects? I have
Possible Duplicate: What's the best method for sanitizing user input with PHP? What are
Possible Duplicate: Which is the best method to perform string concatination in PHP? I
Possible Duplicate: Best method for storing this pointer for use in WndProc I'm trying
Possible Duplicate: What are the best practices for avoiding xss attacks in a PHP
Possible Duplicate: What's the best method for sanitizing user input with PHP? I'm using
Possible Duplicate: The best overloaded method match for 'XDevkit.IXboxDebugTarget.GetMemory(uint, uint, byte[], out uint)' has
Possible Duplicate: Best practices for efficiently storing md5 hashes in mysql We use Hex
Possible Duplicate: best way to get the key of a key/value javascript object foo
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.