473,508 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
8 2607
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
>...
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2548
by: Spam sucks | last post by:
Hello, Could somebody explane me how i can read a session file and unserialize it and read the array that is created. **script for reading session files** function getUsersOnline() { $count...
1
2808
by: Spam sucks | last post by:
Hello, Could somebody explane to me how i can unserialize the content of a session file, i allready reading the session files and the content now i only need to unserialize the data in the...
15
2740
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
2
1595
by: Chris Puncher | last post by:
Hi. I have a RCW class that was generated by the VS.NET2003 IDE when I added a COM dll reference to my project. This all works fine when I call methods on it. My initial problem is that in...
8
1764
by: nephish | last post by:
hey there, is this ok? class MyClass { var $start; var $finish; function MyClass($start, $finish) { $this->start = $start; $this->finish=$finish,
25
1996
by: Marcel | last post by:
I have a person class adn i want to derive an object of that class on one page and pass that object to a next page but that does not work for me and i do not understand why. Here is de code: ...
0
1224
by: Gert Kok | last post by:
I'd like to use 3 types of objects, which have a kind of availability-check as a static object in their parent class. After page reload the initialised member is NULL Is this a PHP 5.2.5...
3
1532
by: notnorwegian | last post by:
i have some confusion over this. sure a class is basically a classification, like for example an animal or flower. and an object/instance of that class is then for example a cat. an object is...
4
4386
by: AndreH | last post by:
Good day, I have the a bit of an issue with retrieving an object from php's session. I set a session variable "user" from the class User as follows: $user = new User(); // ... do some stuff...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7495
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.