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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:19:08+00:00 2026-06-14T19:19:08+00:00

I have a class in which I implement an array attribute. I want to

  • 0

I have a class in which I implement an array attribute. I want to set a couple of objects into that attribute. It will be done during the request flow many times, so it’s declared as static. And it’s not a singleton class.

Will the attribute in that class keep its earlier values when I’ll add something to this the second time?
(adding is done through a static method, if this changes anything)

The example:

/* file1.php */
Foo::add('value1');
include 'file2.php';

/* file2.php */
...
Foo::add('value2');

This is the definition of Foo:

class Foo {
   public static $bar = [];
   public static function add($value)
   {
      Foo::$bar[] = $value;
   }
}

Is this a good practice? Is singleton better here? Is there any other way to deal with 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-06-14T19:19:09+00:00Added an answer on June 14, 2026 at 7:19 pm

    Yes. The include() and require() statements essentially dump the contents of the referenced file into the location of the statement. You can think of it all as one script.

    You can test your question yourself by adding var_dump(Foo::bar); to the end of the script.

    Using the Singleton pattern is only really necessary if you only ever want the class instantiated/initialized once. If you will ever want to have additional instances within a script then you’ll want to leave it as a normal class and start working out how to do what you want. ie: Passing by reference.

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

Sidebar

Related Questions

I have a class that instantiates two classes which implement interfaces. I want one
I have a generic Tree class which will implement ICollection< T > (and hence
I have an array of objects which created from a custom class. The custom
I have a vector-like class that contains an array of objects of type T
I have a class which must implement the following property public ICollection<IType> Items {
I have a factory class, DocumentLoaderFactory , which simply returns an instance that implements
I have a range in java implemented as a class which is divided into
I have an assignment for my architecture class which to implement a 31 backgammon
I have a particular class that stores a piece of data, which implements an
I have the following class which I'm fighting over how to implement. The question

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.