单击href链接到另一个页面时PHP单例丢失实例

单击href链接到另一个页面时PHP单例丢失实例,php,singleton,Php,Singleton,以下是我的情况: 在我的主页上,我通过以下方式初始化我的singleton: mypage.com/index.php <?php include ('includes/userfactory.php'); session_start(); $userFactory = UserFactory::instance(); //make random number 10 to 100 $userFactory->MakeRandomNumber(

以下是我的情况:

在我的主页上,我通过以下方式初始化我的singleton:

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>
<?php
    include ('../includes/userfactory.php');
    session_start();

    $userFactory = UserFactory::instance();
    $userFactory->Print();
?>
<?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

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

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

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

    mypage.com/newlink/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>
    
    <?php
        include ('../includes/userfactory.php');
        session_start();
    
        $userFactory = UserFactory::instance();
        $userFactory->Print();
    ?>
    
    <?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
    
    /**
     * 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() {}
    
    }
    ?>
    
    /**
     * 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() {}
    
    <?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>
    
    <?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>
    
    
    
    问题是在第二页上,甚至还没有生成随机数,所以它创建了另一个假定的单例实例

    这是我的userfactory,它扩展了Singleton类

    userfactory.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>
    
    <?php
        include ('../includes/userfactory.php');
        session_start();
    
        $userFactory = UserFactory::instance();
        $userFactory->Print();
    ?>
    
    <?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
    
    /**
     * 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() {}
    
    }
    ?>
    
    /**
     * 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() {}
    
    <?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>
    
    <?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>
    
    
    
    这是我从线程复制的单例模式

    singleton.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>
    
    <?php
        include ('../includes/userfactory.php');
        session_start();
    
        $userFactory = UserFactory::instance();
        $userFactory->Print();
    ?>
    
    <?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
    
    /**
     * 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() {}
    
    }
    ?>
    
    /**
     * 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() {}
    
    <?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>
    
    <?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>
    
    
    
    我做错了什么?解决了 从singleton.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>
    
    <?php
        include ('../includes/userfactory.php');
        session_start();
    
        $userFactory = UserFactory::instance();
        $userFactory->Print();
    ?>
    
    <?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
    
    /**
     * 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() {}
    
    }
    ?>
    
    /**
     * 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() {}
    
    <?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>
    
    <?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>
    
    使用类UserFactory和Singleton保存$\会话示例 test.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>
    
    <?php
        include ('../includes/userfactory.php');
        session_start();
    
        $userFactory = UserFactory::instance();
        $userFactory->Print();
    ?>
    
    <?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
    
    /**
     * 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() {}
    
    }
    ?>
    
    /**
     * 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() {}
    
    <?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>
    
    <?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>
    
    
    我的页面
    
  • test2.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>
    
    <?php
        include ('../includes/userfactory.php');
        session_start();
    
        $userFactory = UserFactory::instance();
        $userFactory->Print();
    ?>
    
    <?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
    
    /**
     * 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() {}
    
    }
    ?>
    
    /**
     * 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() {}
    
    <?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>
    
    <?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>
    
    
    我的第2页
    
  • 权力大,责任大
    年轻的学徒们,好好利用你们的新能力。

    这是一个需要消化的问题。你能把你的问题重新编排成合适的格式吗?期望我们尝试运行所有这些并发现错误,这有点过分了。@TheGrandJ不幸的是,就是这样。我试图在我的站点的多个页面上访问一个单例实例,并使这个实例在其中唯一。我该怎么做呢?点击一个链接会改变php进程,这样你就不能再通过这样做访问你的单例了。这就像你网站的另一个用户正在访问你的页面一样。我建议将您的对象存储在php会话@JulienBourdic中,感谢您的洞察力!它完美地解决了我的问题,但我的singleton类仍然有一个bug。