首页 > 解决方案 > 单击指向另一个页面的href链接时PHP单例丢失实例

问题描述

这是我的情况:

在我的主页上,我以这种方式初始化我的单例:

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>

当我单击 href 链接导航到另一个页面时,在该页面上我尝试调用我之前创建的单例实例,然后检查我的随机数以查看它是否与以前相同。

mypage.com/newlink/index.php

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

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

问题是在第二页上甚至还没有生成随机数,所以它创建了另一个假设的单例实例。

这是我扩展 Singleton 类的用户工厂。

用户工厂.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);
        }
    }
}
?>

这是我从这个线程复制的单例模式

单例.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() {}

}
?>

我究竟做错了什么?

标签: phpsingleton

解决方案


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.


推荐阅读