inheritance on a serialized object puzzle

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tibo
Forum Newbie
Posts: 2
Joined: Wed Sep 20, 2006 9:36 pm

inheritance on a serialized object puzzle

Post by tibo »

Hi Folks,

Let's say, for this christmas I'm going to offer to selected customers a rebate if they enter a promo code at an online store.

What I want to do is redefine a method on the fly if a promo code is recognized. Here's the scheme :

---STEP 1---
session is created
promo code is entered
a new shopping cart is instanciated
a new user is intanciated
both objects are serialized
(submit)

---STEP 2---
both objects are unserialized
promo code is recognized (switch)
>>> REDEFINE METHOD HERE


So basically, the question is : How to redefine the method of a serialized object ?

If there was no need to serialize, I would simply make a chritmas class extending my cart class.

Do I have to convert the serialized one into another object from my new class ? If so, how would you do that?

(PHP 4.4.3)

Here's some piece of code to illustrate STEP2:

Code: Select all

switch($_SESSION['promocode']) {
	case "happyxmas":
		define(CLASSNAME, 'xmas');
		require_once('class/xmas.class.php');
		break;
	default:
		define(CLASSNAME, 'cart');
		break;
 }

$cart = unserialize($_SESSION['cartObj']);
So if no code is entered, the regular method is:

Code: Select all

//Purchase of a regular product
$cart->purchase_product();
But if the right promo code is entered you get the redefined method:

Code: Select all

//2 for the price of 1  xmas rebate
$xmas_cart->purchase_product();
Hope I'm being clear enough.
Any clue welcome.

Thank you.
:o
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You cannot redefine a method without special help from a PHP extension. Override it via inheritance, sure.

But I fail to see the need here. Simply write the original method to understand what to do in both situations and give it the information to know which to use, or have a method that it calls that contains whatever specific logic you watch to switch around.
Post Reply