470,833 Members | 1,962 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

Object classes

Hi

I need to know how to keep instance of class. For example. I created
object:

$myObj = new Person();

ok, and then someone wants to go to the links page, and I want to keep
data which are created in myObj, for exmaple $myObj->age; $myObj->name;
etc. etc.

I thought about $serMyObj = serialize($myObj) and the put $serMyObj
data through POST, GET, or sth like that, and then GET it, but what if
the portal is big and i want to keep 50 objects in one session ?? put
data to the database and after that get it and every time someone is
redirecting to the another page, get form database ...

Oct 20 '05 #1
6 1240
Python wrote:
Hi

I need to know how to keep instance of class. For example. I created
object:

$myObj = new Person();

ok, and then someone wants to go to the links page, and I want to keep
data which are created in myObj, for exmaple $myObj->age; $myObj->name;
etc. etc.

I thought about $serMyObj = serialize($myObj) and the put $serMyObj
data through POST, GET, or sth like that, and then GET it, but what if
the portal is big and i want to keep 50 objects in one session ?? put
data to the database and after that get it and every time someone is
redirecting to the another page, get form database ...


Hi Python,

I may make more sense for you to use SESSION instead of passing info around
with POST and GET.
Read here:
http://nl3.php.net/session

Be sure you read the about Objects and Session, because there are some
issues if you first start a session, and then declare the object.
First load the objectdefinition, then start the session, but that is all
explained at the php site.

Good luck.

Regards,
Erwin Moller
Oct 20 '05 #2
Python wrote:
Hi

I need to know how to keep instance of class. For example. I created
object:

$myObj = new Person();

ok, and then someone wants to go to the links page, and I want to keep
data which are created in myObj, for exmaple $myObj->age; $myObj->name;
etc. etc.

I thought about $serMyObj = serialize($myObj) and the put $serMyObj
data through POST, GET, or sth like that, and then GET it, but what if
the portal is big and i want to keep 50 objects in one session ?? put
data to the database and after that get it and every time someone is
redirecting to the another page, get form database ...


I think you answered your own question with this post... Use sessions.
You can serialize your object and store it in a session variable. Then
on the next script, make sure you include all the necessary include
files for the object classes before you unserialize. I'd personally use
sessions with custom handlers to utilize database tables for this, but
sometimes that is overkill and takes more resources.

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Oct 20 '05 #3
Justin Koivisto wrote:
I think you answered your own question with this post... Use sessions.
You can serialize your object and store it in a session variable. Then
on the next script, make sure you include all the necessary include
files for the object classes before you unserialize. I'd personally use
sessions with custom handlers to utilize database tables for this, but
sometimes that is overkill and takes more resources.


Just for reference (at least with PHP 4.3+) you don't need to
explicitely serialize/unserialize your objects for use in Sessions. You
can just do:

require_once("foo.class.php");
session_start();
if (!is_object($_SESSION["myfoo"])) {
$_SESSION["myfoo"] = new Foo();
}

$_SESSION["myfoo"]->bar();

Cheers,
Andy
Oct 20 '05 #4
Andy Jeffries wrote:
Justin Koivisto wrote:
I think you answered your own question with this post... Use sessions.
You can serialize your object and store it in a session variable. Then
on the next script, make sure you include all the necessary include
files for the object classes before you unserialize. I'd personally
use sessions with custom handlers to utilize database tables for this,
but sometimes that is overkill and takes more resources.

Just for reference (at least with PHP 4.3+) you don't need to
explicitely serialize/unserialize your objects for use in Sessions. You
can just do:

require_once("foo.class.php");
session_start();
if (!is_object($_SESSION["myfoo"])) {
$_SESSION["myfoo"] = new Foo();
}

$_SESSION["myfoo"]->bar();


You would not believe the number of ISPs still running the 4.0 and 4.1
branches... However, it is good to point that out for those who have
access to newer versions. I had that in my message, but deleted it as
not to confuse the point.

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Oct 20 '05 #5
Justin Koivisto wrote:
Just for reference (at least with PHP 4.3+) you don't need to
explicitely serialize/unserialize your objects for use in Sessions.
You can just do:

require_once("foo.class.php");
session_start();
if (!is_object($_SESSION["myfoo"])) {
$_SESSION["myfoo"] = new Foo();
}

$_SESSION["myfoo"]->bar();

You would not believe the number of ISPs still running the 4.0 and 4.1
branches... However, it is good to point that out for those who have
access to newer versions. I had that in my message, but deleted it as
not to confuse the point.


I'm not sure if it actually worked in older versions, it's been a while
since I converted all my servers to 4.3...

I haven't got any older versions to try it on. Have you tried it on an
older version?

Cheers,
Andy
Oct 20 '05 #6
Andy Jeffries wrote:
Justin Koivisto wrote:
Just for reference (at least with PHP 4.3+) you don't need to
explicitely serialize/unserialize your objects for use in Sessions.
You can just do:

require_once("foo.class.php");
session_start();
if (!is_object($_SESSION["myfoo"])) {
$_SESSION["myfoo"] = new Foo();
}

$_SESSION["myfoo"]->bar();


You would not believe the number of ISPs still running the 4.0 and 4.1
branches... However, it is good to point that out for those who have
access to newer versions. I had that in my message, but deleted it as
not to confuse the point.

I'm not sure if it actually worked in older versions, it's been a while
since I converted all my servers to 4.3...

I haven't got any older versions to try it on. Have you tried it on an
older version?


I had done it once before and did have problems with objects in
sessions, but I don't remember what version of PHP it was - of course,
it could have been that server's setup as well. I've been making a point
of not developing for versions under 4.3 as of late, and soon will be
trying to push php5 much more. (When I've had some more time to play.)

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Oct 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Avi Kak | last post: by
15 posts views Thread by mr.peteryu | last post: by
8 posts views Thread by a | last post: by
reply views Thread by a | last post: by
5 posts views Thread by JH | last post: by
3 posts views Thread by notnorwegian | last post: by
23 posts views Thread by tonytech08 | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.