gumpi
gumpi

Reputation: 281

Unserialized in PHP to restore to object

I'm currently working on zend framework and trying to store serialised version of an object let's say

class customer{
   protected $id;
   public static function getInstanceById( $id )
   {
      $this->id=$id;
   }
   public function getOrders()
   {
       return array('pizza', 'pancake', 'etc');
   }
}

and if I do

$customer = Customer::getInstanceById(1);
$content = serialize( $customer );
file_put_contents('file.txt', $content);

and later on I do

$data = file_get_contents('file.txt');
$customer = unserialize( $data );
$order = $customer->getOrders(); // <<<<<<<

this will throw error.

any idea how to restore the state of the object when unserializing it?

Please help!

Thanks

Upvotes: 2

Views: 1677

Answers (4)

Ollie
Ollie

Reputation: 196

PHP docs is usually pretty thorough for these things. Check out the page on Object Serialization.

Upvotes: 1

Mr Coder
Mr Coder

Reputation: 8186

try this instead

class customer{
   protected $id;
   public static function getInstanceById( $id )
   {
      $customer = new customer();
      $customer->id = $id;
      return $customer;
   }
   public function getOrders()
   {
       return array('pizza', 'pancake', 'etc');
   }

}

Upvotes: 0

Decent Dabbler
Decent Dabbler

Reputation: 22773

The method getInstanceById doesn't return anything. So you are basically serializing null. Furthermore, the method body is invalid. You can't use instance context ($this) in static methods.

And because of this, the error you are receiving is probably not related to the serialize/unserialize procedure, but to the fact that you are using invalid instance context in a static context.

So, if this is the code you intend to use, it doesn't do anything of the sort that I presume you want it to do, e.g. return an instance of customer with the id you pass to getInstanceById.

I presume instead you want something like this:

class customer
{
    protected $id;
    protected $otherProperty1;
    protected $otherProperty2;

    // prevent publically allowing creating instances
    // but force using getInstanceById for example
    protected function __construct( $id, array $otherData = null )
    {
        $this->id = $id;

        // very simplistic example
        $this->otherProperty1 = $otherData[ '$otherProperty1' ];
        $this->otherProperty2 = $otherData[ '$otherProperty2' ];
    }

    // this static method now WILL return an instance of customer
    public static function getInstanceById( $id )
    {
        $data = self::retrieveInstanceDataFromSomePersistenceStorage( $id );

        // very simplistic and dirty example;
        return new self( $data[ 'id' ], $data );
    }
}

Upvotes: 0

user517491
user517491

Reputation:

The class (customer in this case) needs to be loaded before calling the unserialize() method --

Forexample

include("customer.php"); // consider that customer class is defined the file customer.php

$data = file_get_contents('file.txt');
$customer = unserialize( $data );
$order = $customer->getOrders(); // <<<<<<<

Upvotes: 2

Related Questions