Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1104909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:32:59+00:00 2026-05-17T01:32:59+00:00

I have an object, based on the Singleton design, that I use for user

  • 0

I have an object, based on the Singleton design, that I use for user authentication. Because the object is a per-user object, I want the object to be stored automatically in a session variable at the end of execution. However, any time I try to serialize the object, either internally or externally, I get an empty string.

The following is the basic class, minus irrelevant functions:

<?php
/**
* The user class is intended to handle information about authenticated users. Information contained in this class is to be stored
* in SESSION['session_user'] as a serialized object.
*/
class User {
 // Reference to the single User instance
 private static $_instance;
 // User levels
 const GUEST = 0;
 const USER = 1;
 const ADMINISTRATOR = 3;
 // Information about the account
 private $_username;
 private $_userid;
 private $_userlevel;
 // Information about the user, for preventing session hijacking
 private $_ipaddress;
 private $_useragent;

 private function __construct() {
  // Set the visitor's information
  // Set the default information
 }

 public static function getUser() {

  // Check if a user object has been created
  if (!isset(self::$_instance)) {
   // Check if the object is stored in the user session
   if (isset($_SESSION['session_user'])) {
    self::$_instance = unserialize($_SESSION['session_user']);
    //unset($_SESSION['session_user']);
    echo 'Unserializing user';
   } else {
    $c = __CLASS__;
    self::$_instance = new $c;
    echo 'Creating new user';
   }
 }
 return self::$_instance;
}

function __wakeup() {
 // First, check that the user agent has not changed
 // Check that the IP has not changed
}

function __destroy() {
 $_SESSION['session_user'] = serialize(self::$_instance);
 echo serialize(self::$_instance);
 print_r($_SESSION);
}
public function __set($index, $value) {
 return NULL;
}
public function __get($index) {
 // Determine which value to return
}
public function authenticate($inUsername, $inPassword) {
 // Authenticate the user
}
}
?>

Any time I call serialize on the object, either internally in the __destroy method using serialize($this) or serialize(self::$_instance), or externally using serialize($user), I get an empty string. However, I know the object exists since I can get data out of it about an authenticated user.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:33:00+00:00Added an answer on May 17, 2026 at 1:33 am

    The magic function is called __destruct, not __destroy. Done 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an POCO object and based on the number input I want to
I have a class that is effectively a object based enum. There is a
I have a page based on a model object, and I want to have
I have a method in a singleton class that need to use the .NET
I have no clue how i can get an existing object structure based on
I have a SqlCommand object on my c# based asp.net page. The SQL and
Background: I have an MVC based polaroid object. The model keeps the photo's metadata,
I have an IEnumerable object. I would like to access based on index for
I have a plain html web page with an embedded flash object. Based on
I have created a system that automatically registers function objects (functors) into a map

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.