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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP & OOP

Status
Not open for further replies.

vladibo

Programmer
Joined
Sep 14, 2003
Messages
161
Location
CA
Well, I know that in PHP there is no such thing as global table of objects. This means that any times one object is used, it is recreated again. OK, but what if I place this object in session? Shall it be recreated again?

Thank you in advance.
 
When you store an object in a session, PHP stores a snapshot of that object's datastore.

When the session is read by the next script, the object is recreated.

The one "gotcha" is that the class definition for the object must appear in code before the session_start() function is invoked.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Ohhh that's not good...
 
Because using objects is expesive :( But I love working with objects. Unfortunately I can not use java and c# for my current projects... Let us hope php 5 will be wiser
 
What makes you think objects are expensive in PHP?

Given the following three files and pointing my browser to test_session_object1.php:

test_session_object.inc:
Code:
<?php
class foo
{
	var $fu;
	
	function foo($fuvar)
	{
		$this->fu = $fuvar;
	}
	
	function display()
	{
		print '!!!' . $this->fu . '!!!';
	}
}
?>

test_session_object1.php:
Code:
<?php
include ('test_session_object.inc');

session_start();

$_SESSION['sessfu'] = new foo('a foo');

print '<html><body><a href=&quot;test_session_object2.php&quot;>click here</a></body></html>';
?>

test_session_object2.php:
Code:
<?php
include ('test_session_object.inc');

session_start();

print '<html><body>';

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

print '</body></html>';
?>

There is no overhead above and beyond having to include the class-definition file to using sessions with objects versus sessions with simple variables.

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

Part and Inventory Search

Sponsor

Back
Top