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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:47:17+00:00 2026-05-27T22:47:17+00:00

This is how I’ve set up my Singleton <?php class MySingleton { private static

  • 0

This is how I’ve set up my Singleton

<?php
    class MySingleton
    {
        private static $instance;
        private static $you;

        private function __construct()
        {
            $this->you = "foo";     
        }

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

        public function getYou()
        {
            return $this->you;
        }       
        public function setYou($val)
        {
            $this->you = $val;
        }

    }
?>

In file1.php, I do this:

require_once('session.php');
$session = MySingleton::singleton();
$session->setYou('bar');
echo $session->getYou(); //echoes 'bar'

In file1.php, I have a hyperlink to file2.php, where I have this code:

require_once('session.php');
$session = MySingleton::singleton();
echo ($session->getYou()); //prints 'foo' which gets set in the constructor

It seems it is creating a new instance for file2, which is why $you has the default value of foo. Where am I going wrong? Why don’t I get the instance I was using in file1.php?

  • 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-27T22:47:18+00:00Added an answer on May 27, 2026 at 10:47 pm

    A singleton in PHP is only valid for the current request (as HTTP is stateless).

    If you want to preserve the state of that object, save it in a session:

    class MySingleton
        {
            private static $instance;
            private static $you;
    
            private function __construct()
            {
                $this->you = "foo";     
            }
    
            public static function singleton()
            {
                session_start();
                if (!($_SESSION['MyInstance'] instanceof MySingleton)) {              
                    $className = __CLASS__;
                    $_SESSION['MyInstance'] = new $className;
                }
                return $_SESSION['MyInstance'];
            }
    
            public function getYou()
            {
                return $this->you;
            }       
            public function setYou($val)
            {
                $this->you = $val;
            }
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This is driving me insane! Here's the code: public static void main(String[] strings) {
This is essentially what I want to do: public abstract class Uniform<T> { public
This code won't compile because there is an illegal reference to a static field.
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
This is the blurb accompanying the Audio Services function AudioServicesSetProperty. Its a bit over
This is my first time creating a PHP form that will run a MySQL
This is a bit of a long shot, but if anyone can figure it

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.