Putting Object In Session [PHP]

Rajesh Paul

Using the following code construct we can put an object in a session variable in PHP. Let’s us create a simple class file c1.php and two php scripts 1.php and 2.php to demonstrate this.


class c1
public $v1=10.5;
private $v2=20;
protected $v3=30;
public $v4="a string";
function f1()
echo "alert('class function f1() called.')";

In 1.php we include the class file c1.php either using __autoload() function or using include() and instantiate a new instance of the class directly into a session variable or assign a named object into a session variable. 


function __autoload($class)

$obj1_c1=new c1();//$obj_c1 is a named object

echo "object in session. Now open 2.php"

Now we have the object in a session variable, we can go on to 2.php and use methods from c1.


<?php function __autoload($class) { include($class.".php"); }…

View original post 24 more words


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s