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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:49:44+00:00 2026-06-06T14:49:44+00:00

In OOP PHP witch is better to let a factory return, new instance or

  • 0

In OOP PHP witch is better to let a factory return, new instance or the same instance, when it comes to performance and unit testing.

Eg. is creating new instances each time a class is to be used considered bad practice?

  • 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-06T14:49:45+00:00Added an answer on June 6, 2026 at 2:49 pm

    You dont want to have Factories return the same instance. You want them to return new instances. That’s the point of a Factory. Singletons have no use in PHP. If you want to limit an instance to just one instance, then create it once and inject it where it is needed.

    Performance-wise it is negligible whether you return the same instance or create a new one. That is, unless you are doing very costly work in the ctor (like connecting to a database), which you shouldnt anyway.

    For unit-testing, you want your Factories to return fresh instances and not the same ones, because that would be global state. Test should happen in isolation and not with the state of some instance from a previous test still in place, because that is more error prone.

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

Sidebar

Related Questions

Hi I'm new to OOP with PHP. I'm wondering how OO comes into play
I'm trying to learn how to use oop in php. I'm also fairly new
I'm relatively new using OOP in PHP. It's helped immensely in the organization and
I am pretty new to OOP with PHP. OK first of all this produces
I'm new to OOP using PHP and the idea seems a little pointless in
I am very new to OOP styled PHP, and I am trying to implement
I'm pretty new to OOP (and also in PHP programming), and I have some
I am learning OOP with PHP. I am creating a class to extract XML
I'm relatively new to PHP OOP and cant seem to be able to call
I'm new to php oop.here i wanted to do database connectivity with singleton class

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.