article

Passing classes from page to page

Email
Submitted on: 1/1/2015 1:29:00 PM
By: Dustin R Davis (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: PHP 4.0
Views: 1850
 
     Ever needed to pass a class and its data from one page to the next? I will show you how.

 
				

 

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
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.