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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:42:01+00:00 2026-05-23T00:42:01+00:00

So I have a main class that calls another singleton class but when running

  • 0

So I have a main class that calls another singleton class but when running multiple threads (or concurrent threads) I get cross data contamination. This is a very simple version to explain the problem. All the variable setter/getters are in the Singleton and are called and set by the main class.

class A {

    public function doSomething($var) {
        Singleton::instance()->setVar($var);
    }

    public function showSomething() {
        return Singleton::instance()->getVar();
    }

}

// Singleton

class Singleton {
    private static $instance = null;
    private $var;

    public static function instance() {
        if(!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }
        return self::$instance;
    }

    public function setVar($var) {
        $this->var = $var;
    }

    public function getVar() {
        return $this->var;
    }
}

test script 1:

$actions = Array(
    'one',
    'two',
    'three',
);

foreach($actions as $act) {
    $action = new A();
    $action->doSomething($act);
    echo "Action: ".$action->showSomething()."\n";
    sleep(2);
}

test script 1 output will have;

one
two
three

test script 2:

$actions = Array(
    '1',
    '2',
    '3',
);

foreach($actions as $act) {
    $action = new A();
    $action->doSomething($act);
    echo "Action: ".$action->showSomething()."\n";
    sleep(2);
}

test script 2 output will have;

1
2
3
one
two
three

(not in this order and it might be missing one of the values)

So why is test 1 included in test 2 results when executing both scripts at the same time?

How I’m testing:

open two terminals, execute one script in each terminal (hence the sleep) so I could see the data contamination.

  • 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-23T00:42:02+00:00Added an answer on May 23, 2026 at 12:42 am

    You write in your question that test1 and test2 are two separate processes. From the example code you give I can not see that both scripts exchange data (not code) in a way that allows to be cross-process exchanged (e.g. via a file, session whatever).

    However from the symptoms it looks like that when you execute test2, test1 is executed as well.

    To better understand what’s going on, you can add code into your singleton class that echo’es outwhat it currently does. E.g. setting a variable or showing it. Then you can trace why your problem occurs. You should pretty fast find out where the problem has it’s root.

    Alternatively if you have a IDE with debugger support at hand, you can do this by stepping through the execution as well.

    class Singleton {
        private static $instance = null;
        private $var;
    
        public static function instance() {
            if(!isset(self::$instance)) {
                $c = __CLASS__;
                self::$instance = new $c;
            }
            echo 'Singleton Instantiated.',"\n";
            return self::$instance;
        }
    
        public function setVar($var) {
            echo 'Singleton::setVar(', $var, ').',"\n";
            $this->var = $var;
        }
    
        public function getVar() {
            echo 'Singleton::getVar(): "', $var, '".',"\n";
            return $this->var;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a main JFrame that calls a class(A) and that class calls another
I have a main class in a program that launches another class that handles
What I'm doing exactly is this: I have a class Welcome that calls another
I have a C# program that uses a class from another assembly, and this
Say I have a singleton-ish, factory-ish, reflection-ish class that receives some input, and spits
I have a question about the 'Event Dispatch Thread'. I have a Main class
I have a small Java test app in Netbeans where the main() class reads
I am working on a class library using C#. I have designed 3 main
I have the following code: public class Test { public static void Main() {
I have the following code: class Program { static void Main(string[] args) { new

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.