|
|
Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. |
Introduction |
The scenario: Lets
say you have a customer who is buying something. The customer is checking
out, because he is done shopping. You have setup two pages, one with his
personal information such as name address etc. and the other with the
billing information. Since you are so organized and neat, you don't want
to jumble the page with form fields so you decided to have two separate
pages.
You think to yourself,
"Wow, that's allot of variables to keep track of!". You ponder
how much work this will be and you finally come up with a solution: You
will use a class to store the information!
After designing and
building a class, you sit back and sip your beer. Unfortunately you cant
enjoy that sip because you realized that you have no idea how you are
going to send the information from page one over to page two!
Well, I'm going to
show you how to move your classes and your data that's held in them from
page to page.
|
The
Class |
This will held in
your class.php file. You will need to include this in every page you want
to access your class and its data.
<?PHP
class cMyClass {
var
$Name;
function
PrintName() {
echo
"Your name is ".$this->Name;
return true;
}
}
?>
|
The
Input page |
This page will be
what you submit your personal information to.
<?PHP
include("./class.php");
$myClass
= new cMyClass;
$myClass->Name
= $HTTP_POST_VARS["Name"];
...
//other code you have
$_SESSION["MyClassData"]
= serialize($myClass);
?>
|
The
last page |
This page will display
their information to them. The information that was carried over from
page to page on the site.
<?PHP
include("./class.php");
$temp
= $_SESSION["MyClassData"];
$myClass
= unserialize($temp);
$myClass->PrintName();
?>
|
Conclusion |
Unfortunately,
due to my laziness, I gave a small, crude example. I think you get the point
though. Once you have designed and built your class, you can save it in
an include file. Make sure to have this file included in every .php file
you will be using your class in. In each page you will need to access your
class data by using unserialize()
and when you are finished you will need to save it again using serialize().
I'm using the session method because I do not have access to write files
on my server, and it would be too hard to write a file for each person that
wants to order something. There are too many people. You can look in the
PHP manual (v10) to figure out how to write them to a file and retrieve
them from a file. |
| |
Other 3 submission(s) by this author
|
|
|
Report Bad Submission |
|
|
Your Vote! |
See Voting Log |
|
Other User Comments |
11/16/2002 3:45:46 PM:Dustin R Davis I hope you enjoy my tutorial. Its
simple but easy. I hope it helps
someone. It was such a pain to figure
this out on my own.
|
11/20/2002 9:23:16 AM:Srirangan Very helpful but Wrongly
Titled!!<br>
Should be "Passing
objects from page to
page"
:-)
|
12/29/2002 10:04:35 AM:Mr. Chuckles You can get the session ID variable and
use that as a file name. I run PHP
under Apache in Windows. The sessions
are stored in the sessions directory
using the session ID as the file name.
If that is good enough for the PHP
developers then it's good enough for
me.
Check if your hosting service
offers MySQL or another database. If
not - move!
You can then write the
serialied object (class - whatever) to
a field in the users record.
Either
way simple things can be used to purge
old data. This is a cool foundation
onto which yo can build things.
|
7/8/2003 8:49:07 PM: Hi,
Thanks heaps for this tut.
Every
time i unserialize my class then
attempt to use its functions i get this
err...
Fatal error: Call to a member
function on a non-object in
c:\inetpub\wwwroot\tgm\admin\index.php
on line 10.
I give there seems to be
no reason for this PLEASE HELP????
|
|
Add Your Feedback! |
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. |
|