簡體   English   中英

單擊 href 鏈接到另一個頁面時,PHP 單例丟失實例

[英]PHP Singleton Loosing Instances when click href link to another page

這是我的情況:

在我的主頁上,我以這種方式初始化我的單身人士:

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

}
?>

我究竟做錯了什么?

解決了

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

使用 UserFactory 和 Singleton 類保存 $_SESSION 示例

測試文件

<?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>

測試2.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>

擁有權利的同時也被賦予了重大的責任

好好利用你的新力量吧,年輕的學徒。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM