Jump to content

Serialize help! How to make changes to the serialized class!


pendraggon87

Recommended Posts

So here is the scenario:

 

I have a class, lets say a user class

 

class User {

  private $name;

  private $address;

 

  public function __construct($name,$address) {

      $this->name = $name;

      $this->address = $address;

  }

}

 

Lets say that I serialize it, and save it into the database.  Then, if I want to change the class structure, how will I essentially update the serialized class?  Instead of making a table in the database with a ton of columns, I wanted to manage many different attributes for the user, and to add more options as needed, but I want to make sure that I can make changes to the class and then save it to the database.

 

Thanks!

Why serialize the class?

 

Why not just reload the information each time?

 

Very basic example:

 


<?php

class User {

private $user;
private $userinfo;

public function __construct($user) {
	$this->user = $user;
	try {
		$this->LoadInfo();
	}
	catch(Exception $e) {
		throw $e;
	}
}

private function LoadInfo() { //I guess you could make this public if you wanted to make it possible to reload infomation....
	//some how retrieve teh database class.... I'm going to assume a singleton on a class named DBClass
	$db = DBClass::GetInst();  //tmp variable to hold the class reference since I'm too lazy to keep typing, and it saves from function calls
	$u = $db->escape($this->user); //tmp variable to hold safe username....  For example, might addslashes() or something....
	if($q = $db->query("SELECT * FROM users WHERE user = '{$u}'")) {
		if($db->num_rows($q)) {
			$r = $db->fetch_assoc(); //there should only be 1 row, so we don't need a loop
			foreach($r as $field => $value) {
				$this->userinfo[$field] = $value;
			}
			return true;
		}
		throw new Exception('User does not exist.');
	}
	throw new Exception ('Error doing user query.');
}

}

?>

 

 

Disclaimer: not the best code in the world (I suck at OOP x.x), and there's better ways to do it...  Just a simple example ;p

 

 

Anyway, then you wouldn't need to serialize the class....  It would just reload the data it needs when it's started....

 

Or, did I misinterpret your question?

corbin, he said he didnt want to do this because of the amount of columns or whatever. I'm guessing he doesn't know about relational database schema [1].

 

I think you should look in to a ORM [2]. Propel [3] is a good example.

 

[1]: http://en.wikipedia.org/wiki/Relational_database

[2]: http://en.wikipedia.org/wiki/Object-relational_mapping

[3]: http://propel.phpdb.org/trac/

 

 

 

corbin, he said he didnt want to do this because of the amount of columns or whatever. I'm guessing he doesn't know about relational database schema [1].

 

I think you should look in to a ORM [2]. Propel [3] is a good example.

 

[1]: http://en.wikipedia.org/wiki/Relational_database

[2]: http://en.wikipedia.org/wiki/Object-relational_mapping

[3]: http://propel.phpdb.org/trac/

 

 

 

 

I know about the relational schema, but I am trying to figure out the most efficient way.  For instance, let us say I have a system that requires three types of users, Administrator, User1, User2, and any person can be a mix of one or all three.  They will have different columns in the database, but it is easier to just serialize an object into a session I guess than to constantly read from a DB.  Would it make more sense to just create a user class that will read once from the database, and then on unload it will save any changes to the database?

Not true.

 

If you want to set up a proper permission scheme you would go with a Role based schema like ACL[1].

 

Edit: Here's another useful entry[2]

 

[1]: http://en.wikipedia.org/wiki/Access_control_list

[2]: http://en.wikipedia.org/wiki/Role-based_access_control

 

corbin, he said he didnt want to do this because of the amount of columns or whatever. I'm guessing he doesn't know about relational database schema [1].

 

I think you should look in to a ORM [2]. Propel [3] is a good example.

 

[1]: http://en.wikipedia.org/wiki/Relational_database

[2]: http://en.wikipedia.org/wiki/Object-relational_mapping

[3]: http://propel.phpdb.org/trac/

 

 

 

 

 

Ahhh.... I was thinking of it differently...

 

He had variable declarations in his code, that I had assumed was going to be pulled from  a database.  So I thought he was storing the serialized version of the class and then pulling it up later....  And then he just didn't know how to handle what would happen if he added more columns and didn't have them in his class....

 

Like I said (maybe it was in a different thread; I say it a lot...), I have  a long way to go with OOP x.x.  Reading the Wikipedia articles you linked to now hehe.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.