Page 1 of 1

inheritance on a serialized object puzzle

Posted: Thu Nov 23, 2006 12:25 pm
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

Posted: Thu Nov 23, 2006 12:52 pm
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.