faqts : Computers : Programming : Languages : PHP : kms : Classes

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

15 of 18 people (83%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do you pass an instance of an object between pages in PHP?
Does the object get passed by value or by reference?

Jun 5th, 2002 04:06
Luc Saffre, Boy Dolphin, Marc Ford,


You must store it in the $_SESSION array.

class A {
  //...
}

if (isset($_SESSION['foo'])) {
  $a = $_SESSION['foo']
} else {
  $a = new A();
  $_SESSION['foo'] = a;
}

Be careful : when session_start() will restore this variable, your 
script must know the class definition. That's why you must put the 
class definition in a separate file and include this file in all pages 
who use $_SESSION (even if they don't access the 'foo' element).