简体   繁体   中英

PHP Singleton Loosing Instances when click href link to another page

Here is my situation:

On my home page I initialize my singleton this way:

mypage.com/index.php

<?php
    include ('includes/userfactory.php');
    session_start();

    $userFactory = UserFactory::instance();
    //make random number 10 to 100
    $userFactory->MakeRandomNumber();
    $userFactory->Print();
?>

<!DOCTYPE html>
<html>
    <head>
        My Page
    </head>

    <body>
        <li class="nav-item"><a href="newlink">go to new link</a></li>
    </body>
</html>

And when I click an href link to navigate to another page, on that page I try to call the singleton instance I previously created and then check my random number to see if it is the same as before.

mypage.com/newlink/index.php

<?php
    include ('../includes/userfactory.php');
    session_start();

    $userFactory = UserFactory::instance();
    $userFactory->Print();
?>

The thing is that on the second page the random number is not even generated yet, so it creates another instance of the supposed singleton.

This is my userfactory which extends Singleton class.

userfactory.php

<?php
include ('singleton.php');

class UserFactory extends Singleton 
{
    private $randomNumber = 0;
    private $isLive = false;

    public function Print()
    {
        echo "My random number is: ".$this->$randomNumber;
    }

    public function MakeRandomNumber()
    {
        if($this->$randomNumber == 0)
        {
            $this->$randomNumber = rand(10,100);
        }
    }
}
?>

And here is my Singleton pattern I copied from this thread

singleton.php

<?php

/**
 * Singleton Pattern.
 * 
 * Modern implementation.
 */
class Singleton
{
    /**
     * Call this method to get singleton
     */
    public static function instance()
    {
        static $instance = false;
        if( $instance === false )
        {
        // Late static binding (PHP 5.3+)
            $instance = new static();
        }
        
        return $instance;
    }

    /**
     * Make constructor private, so nobody can call "new Class".
     */
    private function __construct() {}

    /**
     * Make clone magic method private, so nobody can clone instance.
     */
    private function __clone() {}

    /**
     * Make sleep magic method private, so nobody can serialize instance.
     */
    private function __sleep() {}

    /**
     * Make wakeup magic method private, so nobody can unserialize instance.
     */
    private function __wakeup() {}

}
?>

What am I doing wrong?

SOLVED

Remove these lines from singleton.php

/**
 * Make sleep magic method private, so nobody can serialize instance.
 */
private function __sleep() {}

/**
 * Make wakeup magic method private, so nobody can unserialize instance.
 */
private function __wakeup() {}

Save $_SESSION example using classes UserFactory and Singleton

test.php

<?php
include ('includes/userfactory.php');

session_start();
if(!isset($_SESSION["singleton"]))
{
    $singleton = UserFactory::instance();
    $_SESSION['singleton'] = $singleton;
    $_SESSION['singleton']->MakeRandomNumber();
}else
{
    $_SESSION['singleton']->Print();
}

?>
<!DOCTYPE html>
<html>
    <head>
        My Page
    </head>

    <body>
        <li class="nav-item"><a href="test2.php">TEST2</a></li>
    </body>
</html>

test2.php

<?php
include ('includes/userfactory.php');

session_start();
if(isset($_SESSION["singleton"]))
{
    $_SESSION['singleton']->Print();
}
?>

<!DOCTYPE html>
<html>
    <head>
        My Page 2
    </head>

    <body>
        <li class="nav-item"><a href="test.php">TEST</a></li>
    </body>
</html>

WITH GREAT POWER COMES GREAT RESPONSIBILITY

Use your new powers well young padawans.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM