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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:53:28+00:00 2026-05-29T08:53:28+00:00

Let’s say you have a object that is unique, and it’s used by all

  • 0

Let’s say you have a object that is unique, and it’s used by all other classes and functions …something like $application.

How would you access this object in your functions?

  1. using a global variable in each of you functions:

    global $application;
    $application->doStuff();
    
  2. creating a function, like application() that instantiates the object into a static variable and returns it; then use this function everywhere you need to access the object:

    application()->doStuff();
    
  3. create a singleton thing, like a static method inside the object class which returns the only instance, and use this method to access the object:

    Application::getInstance()->doStuff();
    
  4. KingCrunch & skwee: Pass the application object as argument to each function/class where is needed

    ...
    public function __construct(Application $app, ...){
      ....
    

If there are other options please post them. I’m wondering which of these options is the most efficient / considered “best 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-05-29T08:53:28+00:00Added an answer on May 29, 2026 at 8:53 am

    I’d pass it to all the needed methods.
    i.e.

    function doFoo(Application $app) {
        $app->doStuff();
    }
    

    Both global and singleton considered bad and ties your code too much and this makes unit testing more difficult.
    There is one rule when you are allowed to use singleton, if you answer “yes” to the following statement:

    Do I need to introduce global state to my application AND I must have a single instance of given object AND having more than one instance will cause error

    If you answer yes to all the 3 parts then you can use singleton. In any other case just pass all the instances to all the method who needs them. If you have too much of them, consider using something like Context

    class Context {
        public $application;
        public $logger;
        ....
    }
    ========
    $context = new Context();
    $context->application = new Application();
    $context->logger = new Logger(...);
    doFoo($context);
    ========
    function doFoo(Context $context) {
        $context->application->doStuff();
        $context->logger->logThings();
    }
    

    (you can use getters/setters if you need to protect the data or manipulate it or if you want to use lazy initiation etc).

    Good luck!

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

Sidebar

Related Questions

Let's say that I have classes like this: public class Parent { public int
Let's say I have some content classes like Page, TabGroup, Tab, etc. Certain of
Let's say that I have a set of relations that looks like this: relations
Let's say I'm building a data access layer for an application. Typically I have
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I have a simple Login servlet that checks the passed name and
Let's say I need a 3-digit number, so it would be something like: >>>
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have an facebook application running using the JS SDK. First user
Let's say there is a graph and some set of functions like: create-node ::

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.