By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,853 Members | 2,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,853 IT Pros & Developers. It's quick & easy.

Autoloading class definitions of composed objects on unserialize() from session

P: n/a
Hello everyone!
I'm currently working on a MVC-framework and have run into an issue.

I'm using separate script files to render the active page's CSS and
Javascript Code.
I want to pull the data from the page controller object, which is
initialized in another script.
I thought this wouldn't be a problem if I simply stored the controller
in the session-file, but the unserialization of the object simply
won't work.
The controller has properties that are of different class types than
the controller itsself (page-secific Model and View objects).

My Questions:

1. Are there any problems using $myObj=$_SESSION['myObj']; ?
I thought that as of PHP5 this should work?

2. do class definitions of contained objects also have to be known
when i unserialize?
(like my Model/View objects)

3. can I get __autoload to help me out of this?

4. if not, can i perform some sort of __sleep()/__wakeup() magic to
achieve the same effect?

Any help is very much appreciated, thx.

May 14 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On May 14, 11:53 am, carrion <hofmann.johan...@gmail.comwrote:
Hello everyone!
I'm currently working on a MVC-framework and have run into an issue.

I'm using separate script files to render the active page's CSS and
Javascript Code.
I want to pull the data from the page controller object, which is
initialized in another script.
I thought this wouldn't be a problem if I simply stored the controller
in the session-file, but the unserialization of the object simply
won't work.
The controller has properties that are of different class types than
the controller itsself (page-secific Model and View objects).

My Questions:

1. Are there any problems using $myObj=$_SESSION['myObj']; ?
I thought that as of PHP5 this should work?
Not that I know of.
>
2. do class definitions of contained objects also have to be known
when i unserialize?
(like my Model/View objects)
Yes.
>
3. can I get __autoload to help me out of this?
Absolutely. I highly recommend the use of __autoload() for a lot of
reasons, not the least of which is the problem you've described.

function __autoload($class_name) {
$fn = $class_name . '.php';
require_once $fn;
}


May 14 '07 #2

P: n/a
>...
3. can I get __autoload to help me out of this?

Absolutely. I highly recommend the use of __autoload() for a lot of
reasons, not the least of which is the problem you've described.

function __autoload($class_name) {
$fn = $class_name . '.php';
require_once $fn;

}
Thanks for the quick response.
However, autoload() doesn't do what it should.

The code below is about all that happens in the css skript.

//START
session_start();
$objController=$_SESSION['controller'];
var_dump($objController);
$objCssParser=new CssParser($objController);
//END

Now autoload() is triggered by the "new CSSParser" part and loads the
whole hierarchy of CSSParser's parent classes and interfaces.
But the unserializing doesn't bother it at all. The function is not
called.

So after that the type-hinting in CssParser's constructor causes an
error to be thrown since it can't determine the class of
$objController.

Suggestions?

May 14 '07 #3

P: n/a
On May 14, 12:14 pm, carrion <hofmann.johan...@gmail.comwrote:
...
3. can I get __autoload to help me out of this?
Absolutely. I highly recommend the use of __autoload() for a lot of
reasons, not the least of which is the problem you've described.
function __autoload($class_name) {
$fn = $class_name . '.php';
require_once $fn;
}

Thanks for the quick response.
However, autoload() doesn't do what it should.

The code below is about all that happens in the css skript.

//START
session_start();
$objController=$_SESSION['controller'];
var_dump($objController);
$objCssParser=new CssParser($objController);
//END

Now autoload() is triggered by the "new CSSParser" part and loads the
whole hierarchy of CSSParser's parent classes and interfaces.
But the unserializing doesn't bother it at all. The function is not
called.

So after that the type-hinting in CssParser's constructor causes an
error to be thrown since it can't determine the class of
$objController.

Suggestions?
Try adding this immediately after you define __autoload():

ini_set('unserialize_callback_func', '__autoload');

May 14 '07 #4

P: n/a
On 14 Mai, 18:36, ZeldorBlat <zeldorb...@gmail.comwrote:
On May 14, 12:14 pm, carrion <hofmann.johan...@gmail.comwrote:
>...
3. can I get __autoload to help me out of this?
Absolutely. I highly recommend the use of __autoload() for a lot of
reasons, not the least of which is the problem you've described.
function __autoload($class_name) {
$fn = $class_name . '.php';
require_once $fn;
}
Thanks for the quick response.
However, autoload() doesn't do what it should.
The code below is about all that happens in the css skript.
//START
session_start();
$objController=$_SESSION['controller'];
var_dump($objController);
$objCssParser=new CssParser($objController);
//END
Now autoload() is triggered by the "new CSSParser" part and loads the
whole hierarchy of CSSParser's parent classes and interfaces.
But the unserializing doesn't bother it at all. The function is not
called.
So after that the type-hinting in CssParser's constructor causes an
error to be thrown since it can't determine the class of
$objController.
Suggestions?

Try adding this immediately after you define __autoload():

ini_set('unserialize_callback_func', '__autoload');
Good idea, that almost did the trick.
I say almost since now i get one call to __autoload for the Controller
class that is loaded.
However that also doesn't take care of the inner objects.

Yet... if i can load the containing class, maybe i can use it's
__wakeup method to require the other files as needed...
Anyone tried that? Or is there a chicken/egg problem?

Greetings,

Johannes

May 14 '07 #5

P: n/a
On 14 Mai, 18:47, carrion <hofmann.johan...@gmail.comwrote:
On 14 Mai, 18:36, ZeldorBlat <zeldorb...@gmail.comwrote:
On May 14, 12:14 pm, carrion <hofmann.johan...@gmail.comwrote:
...
3. can I get __autoload to help me out of this?
Absolutely. I highly recommend the use of __autoload() for a lot of
reasons, not the least of which is the problem you've described.
function __autoload($class_name) {
$fn = $class_name . '.php';
require_once $fn;
}
Thanks for the quick response.
However, autoload() doesn't do what it should.
The code below is about all that happens in the css skript.
//START
session_start();
$objController=$_SESSION['controller'];
var_dump($objController);
$objCssParser=new CssParser($objController);
//END
Now autoload() is triggered by the "new CSSParser" part and loads the
whole hierarchy of CSSParser's parent classes and interfaces.
But the unserializing doesn't bother it at all. The function is not
called.
So after that the type-hinting in CssParser's constructor causes an
error to be thrown since it can't determine the class of
$objController.
Suggestions?
Try adding this immediately after you define __autoload():
ini_set('unserialize_callback_func', '__autoload');

Good idea, that almost did the trick.
I say almost since now i get one call to __autoload for the Controller
class that is loaded.
However that also doesn't take care of the inner objects.

Yet... if i can load the containing class, maybe i can use it's
__wakeup method to require the other files as needed...
Anyone tried that? Or is there a chicken/egg problem?

Greetings,

Johannes
by the way, i can't post for an hour or so.

May 14 '07 #6

P: n/a
On 14.05.2007 17:53 carrion wrote:
Hello everyone!
I'm currently working on a MVC-framework and have run into an issue.

I'm using separate script files to render the active page's CSS and
Javascript Code.
I want to pull the data from the page controller object, which is
initialized in another script.
I thought this wouldn't be a problem if I simply stored the controller
in the session-file, but the unserialization of the object simply
won't work.
This is actually far from "simple". PHP wasn't designed with object
persistence in mind and your best bet is to follow its "share nothing"
approach.
The controller has properties that are of different class types than
the controller itsself (page-secific Model and View objects).
Just rebuild it from scratch using parameters passed in request or session.
>
My Questions:

1. Are there any problems using $myObj=$_SESSION['myObj']; ?
I thought that as of PHP5 this should work?

2. do class definitions of contained objects also have to be known
when i unserialize?
(like my Model/View objects)
Yes. See http://www.php.net/manual/en/languag...ialization.php
>
3. can I get __autoload to help me out of this?
__autoload is a hack, you'd better stay away from it. ;)
>
4. if not, can i perform some sort of __sleep()/__wakeup() magic to
achieve the same effect?
IIRC, wakeup() is called when object is already constructed, not when
the classes are loading.
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 14 '07 #7

P: n/a
At Mon, 14 May 2007 09:47:15 -0700, carrion let his monkeys type:
On 14 Mai, 18:36, ZeldorBlat <zeldorb...@gmail.comwrote:
>On May 14, 12:14 pm, carrion <hofmann.johan...@gmail.comwrote:
>...
3. can I get __autoload to help me out of this?
Absolutely. I highly recommend the use of __autoload() for a lot
of reasons, not the least of which is the problem you've described.
function __autoload($class_name) {
$fn = $class_name . '.php';
require_once $fn;
}
Thanks for the quick response.
However, autoload() doesn't do what it should.
The code below is about all that happens in the css skript.
//START
session_start();
$objController=$_SESSION['controller']; var_dump($objController);
$objCssParser=new CssParser($objController); //END
Now autoload() is triggered by the "new CSSParser" part and loads the
whole hierarchy of CSSParser's parent classes and interfaces. But the
unserializing doesn't bother it at all. The function is not called.
So after that the type-hinting in CssParser's constructor causes an
error to be thrown since it can't determine the class of
$objController.
Suggestions?

Try adding this immediately after you define __autoload():

ini_set('unserialize_callback_func', '__autoload');

Good idea, that almost did the trick. I say almost since now i get one
call to __autoload for the Controller class that is loaded.
However that also doesn't take care of the inner objects.
Yet... if i can load the containing class, maybe i can use it's __wakeup
method to require the other files as needed... Anyone tried that? Or is
there a chicken/egg problem?

Greetings,

Johannes
Perhaps you can store class info (use get_class() on the parsed
$objController inside the CssParser class definition) so you can
reconstruct it. If not, the object becomes stdClass.

Not my sharpest hour today, so I may be way off the mark here. Perhaps
having another look at:
http://www.php.net/manual/en/languag...ialization.php yields some
useful insights.

Sh.
May 14 '07 #8

P: n/a
On 14 Mai, 22:29, Schraalhans Keukenmeester <inva...@invalid.spam>
wrote:
At Mon, 14 May 2007 09:47:15 -0700, carrion let his monkeys type:


On 14 Mai, 18:36, ZeldorBlat <zeldorb...@gmail.comwrote:
On May 14, 12:14 pm, carrion <hofmann.johan...@gmail.comwrote:
>...
3. can I get __autoload to help me out of this?
Absolutely. I highly recommend the use of __autoload() for a lot
of reasons, not the least of which is the problem you've described.
function __autoload($class_name) {
$fn = $class_name . '.php';
require_once $fn;
}
Thanks for the quick response.
However, autoload() doesn't do what it should.
The code below is about all that happens in the css skript.
//START
session_start();
$objController=$_SESSION['controller']; var_dump($objController);
$objCssParser=new CssParser($objController); //END
Now autoload() is triggered by the "new CSSParser" part and loads the
whole hierarchy of CSSParser's parent classes and interfaces. But the
unserializing doesn't bother it at all. The function is not called.
So after that the type-hinting in CssParser's constructor causes an
error to be thrown since it can't determine the class of
$objController.
Suggestions?
Try adding this immediately after you define __autoload():
ini_set('unserialize_callback_func', '__autoload');
Good idea, that almost did the trick. I say almost since now i get one
call to __autoload for the Controller class that is loaded.
However that also doesn't take care of the inner objects.
Yet... if i can load the containing class, maybe i can use it's __wakeup
method to require the other files as needed... Anyone tried that? Or is
there a chicken/egg problem?
Greetings,
Johannes

Perhaps you can store class info (use get_class() on the parsed
$objController inside the CssParser class definition) so you can
reconstruct it. If not, the object becomes stdClass.

Not my sharpest hour today, so I may be way off the mark here. Perhaps
having another look at:http://www.php.net/manual/en/languag...tion.phpyields some
useful insights.

Sh.
Thanks for the input.
I got around the issue by rendering the css and js code in the main
view and storing them in the session as strings,
passing the session-id to the generator scripts via GET-Parameter.

Anyway I'm a bit dissapointet that there seems to be no reliable way
of serializing complex objects in PHP5...
Does anybody know if this will improve with PHP6 ?

May 15 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.