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

  • Home
  • SEARCH
  • 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 325285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:12:10+00:00 2026-05-12T09:12:10+00:00

I am writing a fairly complex PHP applications where a single user action can

  • 0

I am writing a fairly complex PHP applications where a single user action can trigger changes in many other sub-systems, and I’m contemplating using an observer pattern. However, I am wondering if I have to re-create all the objects involved.

Is it possible to while serializing objects to store their relationships? For example

$equipmentHandler = new EquipmentHandler();
$character = new Character();
$character->subscribeOnEquipmentChanged($equipmentHandler);

$_SESSION['character'] = serialize($character);
$_SESSION['subscriber'] = serialize($equipmentHandler);

Will the relationship be preserved after unserializing? Or do I have do lump them all into one object?

$cache['character'] = $character;
$cache['subscriber'] = $equipmentHandler;
$_SESSION['cache'] = serialize($cache);

Any advice would be appreciated.

(PS. The character data requires many DB requests to create and I am thinking of storing it by doing a write to cache and DB, but only read from cache policy, so it will be serialized anyway)

  • 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-12T09:12:10+00:00Added an answer on May 12, 2026 at 9:12 am

    A relation will be kept, but it will be different than you expect. When you serialize two instances of Character that both refer to the same EquipmentHandler, you’re going to get two separate instances of this EquipmentHandler, instead of the single one you expected. As this example illustrates:

    <?php
    
    echo "BEFORE SERIALIZE:\n";
    class A { }
    class B { }
    
    $a = new A;
    $b = new B;
    $a -> b = $b;
    
    $a2 = new A;
    $a2 -> b = $b;
    
    var_dump($a->b);
    var_dump($a2->b);
    
    echo "AFTER SERIALIZE:\n";
    $a3 = unserialize(serialize($a));
    $a4 = unserialize(serialize($a2));
    
    var_dump($a3->b);
    var_dump($a4->b);
    

    The output of this is:

    BEFORE SERIALIZE:
    object(B)#2 (0) {
    }
    object(B)#2 (0) {
    }
    AFTER SERIALIZE:
    object(B)#5 (0) {
    }
    object(B)#7 (0) {
    }
    

    Look for the number after the pound. This refers to the object ID within PHP. Before serializing both $a->b and $a2->b refer to an object with object ID #2: the same instance. But after the serialization they refer to object IDs #5 and #7: different instances.

    This may, or may not, be a problem for you.

    To restore the connection to one single B object, you’re going to have to get a little tricky. You could use the __sleep() handler in A to flatten the actual reference to an INSTANCE of B to just a mentioning of B: “I had a reference to B”. Then implement the __wakeup() handler using that mentioning of a B instance in A to acquire a single instance of a new B object.

    BTW. The PHP session extension already does serializing automatically, no need for you to pre-serialize it yourself 🙂

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

Sidebar

Related Questions

No related questions found

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.