Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP classes - Newbie question 1

Status
Not open for further replies.

mjjks

Programmer
Jun 22, 2005
138
US
Hi there,
Being a complete newbie to PHP, I have a generic question.

I have a project that client wants to be done in PHP.
Ideally I'd like to use classes/objects like in ASP.Net (VB.Net), however I don't know how classes do persist in PHP.

In classic ASP I could create a class on page load, but as soon as user would leave page, class would be destroyed.
Is it same in PHP? How do I initiate class that will persist through entire user session?

Anyone can explain it briefly?

Thanks much

Steve
 
As a general rule, any time I need information to be kept available throughout a user's session on a site, I use session variables. And you can store an object in a session variable just as readily as any other datatype.

There is one "gotcha"....

In a standard PHP environment, session variables are not instantiated automatically. Your scripts must, at the very beginning of each script, issue:

session_start();

However, when you store an object in a session variable, it is important that the object definition be already loaded before you issue session_start().



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks for the reply sleipnir214.
Maybe I didn't ask it right way, but I was meaning something different.

Say I have class Project and it has properties UserName, Department and function AddNewUser.

I want to instantiate this class when user logs in through login page and on subsequent pages use its functions and read/write to its properties.

Is this possible in PHP and if yes, any examples?


Thanks
 
Please be specific.

Do you want to instantiate this class into a new object for each script, or do you want to reinstantiate the same object for each script?


Want the best answers? Ask the best questions! TANSTAAFL!
 
As I said, I'm new to PHP, so bare with me while I try to explain.

My background is in VB6 and though it's not true OO language I could define class and instantiate it when I'd start application and use its properties /methods throughout app.

I'd like to instantiate class into object when user goes through entry page/script and retain assigned properties on subsequent pages. So, I guess it would be "reinstantiate the same object for each script".

I hope I cleared confusion a little.

Thanks

 
Then I have to go back to my "use session variables" advice.

For a simple example:

Assume the existence of three scripts:

test_class.php:
Code:
<?php
class name
{
	var $name;
	
	function name ($value = '')
	{
		$this->name = $value;
	}
	
	function display ()
	{
		print $this->name;
	}
}
?>

test_use_class1.php:
Code:
<?php
include ('test_class.php');

session_start();

$_SESSION['person'] = new name('fred');

print '<html><body>';

$_SESSION['person']->display();

print '<br><a href="/test_use_class2.php">click here</a></body></html>';
?>

test_use_class2.php:
Code:
<?php
include ('test_class.php');

session_start();

$_SESSION['person']->display();
?>


If you run test_use_class1.php, you will get output of "fred" and a link.

Clicking on the link will take you to test_use_class2.php, which will reinstantiate the object and output the value again.




Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks sleipnir214. I tried your example and it works fine.

So, basically it's combination with sessions to retain values between pages/scripts. Probably using just sessions would be simpler?

Thanks much.

Steve
 
You're going to have to use session variables.

But whether those session variables are objects or not depends on how you want to write your code and what you're going to do with that code once you've written it.


Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top