English 中文(简体)
PHP object Singleton not working
原标题:

I am programming a website backend in PHP and I need to encapsulate $_SESSION in a class. I ve made my Session class a singleton but I am having trouble using it.

class Session
{

    private static $instance;

    public static $sessionID;

      private function __construct()
      {
          session_start();
          self::$sessionID = session_id();
      }

      public function Session() {
          return Session::singleton();
      }

      public static function singleton()
      {
          if (!isset(Session::$instance)) {
              Session::$instance = new Session();
          }

          return Session::$instance;
      }

      public function destroy()
      {
          foreach ($_SESSION as $var => $val) {
              $_SESSION[$var] = null;
          }

          session_destroy();
      }

      public function __clone()
      {
          trigger_error( Clone is not allowed for  .__CLASS__,E_USER_ERROR);
      }

      public function __get($var)
      {
          return $_SESSION[$var];
      }

      public function __set($var,$val)
      {
          return ($_SESSION[$var] = $val);
      }

      public function __destruct()
      {
          session_write_close();
      }

      public function exist( $var ) {
        return isset($_SESSION[ $var ]);
      }

 }

Works fine 95% of the time. But occasionnaly when I invoke var_dump(Session::singleton());

output : object(Session)#2 (0) { }

It seems to me obvious that it is making another instance of my Session class, but I don t see exactly how it is possible.

Thanks a lot!

问题回答

Try removing the public constructor (leaving only the private one)

  public function Session() {
      return Session::singleton();
  }

I m not 100% sure that will do it but having a public constructor does not adhere to the Singleton pattern

The #2 (after object(Session) ) is not number of instance of Session class but it s next number of object created by php. If You get #2 it seems that You ve created something(object) before. I ve tested Your class and when I run "var_dump(Session::singleton());" I get: "object(Session)#1 (0) { }" but when I run it at end of script I get (for example): "object(Session)#31 (0) { }"

Regards





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签