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 8980469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:05:45+00:00 2026-06-15T20:05:45+00:00

If I do this in PHP 5.x (generally pseudo-code): class object{ __construct($id){ <<perform database

  • 0

If I do this in PHP 5.x (generally pseudo-code):

class object{
    __construct($id){
       <<perform database query>>
    }
}

$array(1) = new object(1);
$array(1) = new object(1);

Am I wasting efforts (memory, cpu, etc)? Will the database query run twice?

What is the best way to avoid this (if it’s a concern)? Using $array(1) instanceof object? isset($array(1))? array_key_exists(1, $array)?

Is there a (good?) way to check in the class to see if it already exists before it does all the work again?


EDIT: For clarity, I do not need this object twice. And this object will be used again $array(523) = new object(523); My main question is how best to avoid redundantly creating the exact same object.

  • 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-15T20:05:47+00:00Added an answer on June 15, 2026 at 8:05 pm

    If you want to avoid creating multiple objects, you have a few choices. The ones that come to mind are…

    1. Create the object once and ensure it’s passed to where it’s needed. This means you’ll have to check the dependencies in your code with respect to that object.

    2. Use an object factory to construct the object and it can return a reference to the object rather than creating a new one, if it was already created.

    3. Implement the singleton pattern.

    As with everything else, trade-offs obtain, so check your situation and decide what is the most appropriate for you.

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

Sidebar

Related Questions

I have this PHP code to highlight the Query on search results. if (isset($_REQUEST['k'])){
I am using this PHP code: if (isset($_GET['c'])) { $pages = array(home, upload, signup);
Disclaimer: I'm new to object-oriented PHP and I'm generally terrible at the language. So
I got this PHP code: // connect to mysql require_once('includes/connect.php'); // include config include('includes/config.php');
I've got this php code to send the users information from the contact form
I found this PHP code in an app I have to modify... $links =
So I'm new to PHP and MySQL generally, and MVC applications. Forgive me if
I'm working with a [generally not widely used] PHP framework in which the class
Every path through the PHP code might generate a different HTML page. I generally
I'm writing this in PHP, but, generally speaking, what methods and properties would 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.