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

  • Home
  • SEARCH
  • 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 6108737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:19:39+00:00 2026-05-23T14:19:39+00:00

I’m working on a complex project in PHP and keep running into the same

  • 0

I’m working on a complex project in PHP and keep running into the same problem: How to keep separate objects separate?

The idea behind OO programming is that none of the objects need to know how any other object works internally. But with a 3rd normal database, everything is in a separate table and all of the tables are interconnected.

Let’s say I have several objects. An Order object (with an Order class, and several Order related database tables), Address objects with an Address Class and Address tables), Items (tables and class) and Customers (table and class).

Many tasks, such as shipping an order, require data from all of the above tables, and all of the data is interconnected via foreign keys. So what is the most elegant/efficient way to load all of the objects/data needed, without making the code a disaster or violating OO principals?

Approach A:
One class does a single query and loads other classes into itself.

class Order {

   public function LoadFromID($OrderID)
   {
      SELECT * FROM Orders
      LEFT JOIN Customers
      LEFT JOIN Addresses
      LEFT JOIN Items

      $this->oCustomer = new Customer();
      $this->oCustomer->SetName($W->CustomerName);

      $this->oAddress = new Address();
      $this->oAddress->SetCity($W->City);

      $this->oaItems = array();
      foreach($w->Items AS $Item)
      {
         $oItem = new Item();
         $oItem->SetName($Item->ItemName);
         $this->oaItems[] = $oItem;
      }
   }
}

The problem is that the Order class needs to be aware of how the Customers, Items, and Addresses tables are laid out. What if we want to go the other way and have the address object find Orders and Vendors that are attached to it? Since everything is interlinked, every object would need to know how every other object’s tables are structured. Which means if you wanted to add a new field to a table, you would have to find and edit those queries inside of every single object. Which creates a huge maintenance problem.

Approach B:
One class instantiates itself and directs other classes to instantiate themselves within itself.

class Order {

   public function LoadFromID($OrderID)
   {
      SELECT * FROM Orders
      $W = mysql_row();

      // A customer object is instantated and passed
      // a customer ID to load from.
      $this->oCustomer = new Customer($W->CustomerID); 
      $this->oShipAddress = new Address($W->ShipAddressID);
      $this->oBillAddress = new Address($W->BillAddressID);

      $this->oaItems = array();
      foreach($w->Items AS $Item)
      {
         $this->oaItems[] = new Item($Item->ItemID);
      }
   }
}

class Customer{
   public function __construct($CustID)
   {
      SELECT * FROM Customers WHERE $CustID;
      $W = mysql_row();

      $this->CustomerName = $W->Name;
      ...
   }
}

This method keeps objects separate, where only a class can access its own tables, but introduces new problems. First, it’s inefficient from a MySQL standpoint, doing many queries where one could get all the needed data. Not sure if the performance is negligible or not. Also, an object never knows whether it was created by a controller or another object since objects can be spawned anywhere. This is particularly problematic when you need to use transactions that wrap around multiple objects doing multiple things. Where does the call to “begin” and “commit” go? As an object, am I already inside of a transaction, or do I need to start one myself? This inconsistency can clash as method are used for more than one specific task. Sometimes a method needs to create the transaction, other times it’s already inside of it.

Approach C:
The controller instantiates all objects.

class Controller {

   public function DoSomething()
   {
      $DB->Begin();
      $oOrder=new Order();
      $aAddressIDs = $oOrder->GetAddressIDs();
      $CustomerID = $oOrder->GetCustomerID();

      foreach($aAddressIDs AS $ID)
      {
         // Take the order's address IDs 
         // and turn them into adress objects then 
         // pass them back to the order.
         $oAddress = new Address($ID);
         $oOrder->AddAddress($oAddress);
      }

      // Create a customer ID for the order
      // and pass the built object back into the order.
      $oCustomer = new Customer($CustomerID);
      $oOrder->AddCustomer($oCustomer);

      if($oOrder->DoSomethingComplex())
         $DB->Commit();
      else
         $DB->Rollback();
   }
}

This solves the problem of consistency regarding starting and completing database transactions by placing them outside the objects. Also, there’s no confusion over where an object was instantiated, since all objects must be created by a controller. But this is really sloppy looking, and requires each controller method to know what the Order object needs in order to function.

Is there any clean way to do this?

  • 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-05-23T14:19:41+00:00Added an answer on May 23, 2026 at 2:19 pm

    Sorry, you have felled into a well known pitfall.
    Objects are not supposed to mirror the DB.
    The order object needs to get all relevant data to process the order into himself.
    He absolutely can use other classes/objects (A tree object to get the order items correctly ordered, a db connection class, a Sql abstraction class etc)
    If you can, you should load the data in one query. Not one query per table.
    If you want to go with the paradigm of one query per one data source I would say that a relational DB is not what you want, but rather a NoSQL DB (like MongoDB, for example), but there too, you will need to use but one object to handle the order, as the MongoDB pretty much enforces this.

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

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I want to count how many characters a certain string has in PHP, but
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.