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 7408493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:55:38+00:00 2026-05-29T05:55:38+00:00

I’ve worked with PHP for a few years now, but up until now never

  • 0

I’ve worked with PHP for a few years now, but up until now never had a need to deal with serialisation explicitly, only using the $_SESSION. Now I have a project that requires me to manually implement serialisation mechanism for certain data – and I realise that the issue is applicable to $_SESSION as well.

I have a class that contains a number of properties. Most of these properties are small (as in memory consumption): numbers, relatively short strings, etc. However the class also contains some properties, which may contain HUGE arrays (e.g. an entire dump of a database table: 100,000 rows with 100 fields each). As it happens, this is one of the classes that needs to be serialised/deserialised – and, luckly, the properties containing large arrays don’t need to be serialised, as they are essentially temporary pieces of work and are rebuilt anyway as necessary.

In such circumstances in Java, I would simply declare the property as transient – and it would be omitted from serialisaion. Unfortunately, PHP doesn’t support such qualifiers.

One way to deal with is it to have something like this:

class A implements Serializable
{
    private $var_small = 1234;
    private $var_big = array( ... );  //huge array, of course, not init in this way

    public function serialize()
    {
        $vars = get_object_vars($this);
        unset($vars['var_big']);
        return serialize($vars);
    }

    public function unserialize($data)
    {
        $vars = unserialize($data);
        foreach ($vars as $var => $value) {
            $this->$var = $value;
        }
    }
}

However this is rather cumbersome, as I would need to update serialize method every time I add another transient property. Also, once the inheritance comes into play, this becomes even more complicated – to deal with, as transient properties may be in both subclass and the parent. I know, it’s still doable, however I would prefer to delegate as much as possible to the language rather than reinvent the wheel.

So, what’s the best way to deal with transient properties? Or am I missing something and PHP supports this out of the box?

  • 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-29T05:55:38+00:00Added an answer on May 29, 2026 at 5:55 am

    Php provides __sleep magic method which allows you to choose what attributes are to be serialized.

    EDIT I’ve tested how does __sleep() work when inheritance is in the game:

    <?php
    
    class A {
        private $a = 'String a';
        private $b = 'String b';
    
        public function __sleep() {
            echo "Sleep A\n";
            return array( 'a');
        }
    }
    
    class B extends A {
        private $c = 'String c';
        private $d = 'String d';
    
        public function __sleep() {
            echo "Sleep B\n";
            return array( 'c');
        }
    }
    
    class C extends A {
        private $e = 'String e';
        private $f = 'String f';
    
        public function __sleep() {
            echo "Sleep C\n";
            return array_merge( parent::__sleep(), array( 'e'));
        }
    }
    
    $a = new A();
    $b = new B();
    $c = new C();
    
    echo serialize( $a) ."\n";  // Result: O:1:"A":1:{s:4:"Aa";s:8:"String a";}
    // called "Sleep A" (correct)
    
    echo serialize( $b) ."\n"; // Result: O:1:"B":1:{s:4:"Bc";s:8:"String c";}
    // called just "Sleep B" (incorrect)
    
    echo serialize( $c) ."\n"; // Caused: PHP Notice:  serialize(): "a" returned as member variable from __sleep() but does not exist ...
    
    // When you declare `private $a` as `protected $a` that class C returns:
    // O:1:"C":2:{s:4:"*a";s:8:"String a";s:4:"Ce";s:8:"String e";}
    // which is correct and called are both: "Sleep C" and "Sleep A"
    

    So it seems that you can serialize parent data only if it’s declared as protected :-/

    EDIT 2 I’ve tried it with Serializable interface with following code:

    <?php
    
    class A implements Serializable {
        private $a = '';
        private $b = '';
    
        // Just initialize strings outside default values
        public function __construct(){
            $this->a = 'String a';
            $this->b = 'String b';
        }
    
        public function serialize() {
            return serialize( array( 'a' => $this->a));
        }
    
        public function unserialize( $data){
            $array = unserialize( $data);
            $this->a = $array['a'];
        }
    }
    
    class B extends A {
        private $c = '';
        private $d = '';
    
        // Just initialize strings outside default values
        public function __construct(){
            $this->c = 'String c';
            $this->d = 'String d';
            parent::__construct();
        }
    
        public function serialize() {
            return serialize( array( 'c' => $this->c, '__parent' => parent::serialize()));
        }
    
        public function unserialize( $data){
            $array = unserialize( $data);
            $this->c = $array['c'];
            parent::unserialize( $array['__parent']);
        }
    }
    
    $a = new A();
    $b = new B();
    
    echo serialize( $a) ."\n";
    echo serialize( $b) ."\n";
    
    $a = unserialize( serialize( $a)); // C:1:"A":29:{a:1:{s:1:"a";s:8:"String a";}}
    $b = unserialize( serialize( $b)); // C:1:"B":81:{a:2:{s:1:"c";s:8:"String c";s:8:"__parent";s:29:"a:1:{s:1:"a";s:8:"String a";}";}}
    
    
    print_r( $a);
    print_r( $b);
    
    /** Results:
    A Object
    (
        [a:A:private] => String a
        [b:A:private] => 
    )
    B Object
    (
        [c:B:private] => String c
        [d:B:private] => 
        [a:A:private] => String a
        [b:A:private] => 
    )
    */
    

    So to sum up: you can serialize classes via __sleep() only if they don’t have private members in super class (which need to be serialized as well). You can serialize complex object via implementing Serializable interface, but it brings you some programming overhead.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.