how to store object in request scope

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alaa56mm
Forum Newbie
Posts: 3
Joined: Wed Jul 18, 2007 7:33 am

how to store object in request scope

Post by alaa56mm »

dear all..
iam J2EE developer,and new tophp,in jsp , there are a functionality to store object (e.g, UserVO) on request scope,like

Code: Select all

request.setAttribute(,
i mean when i am at a page1.php, and want to go to page2.php,i should store Object on request Scope when iam in page1.php, then getting this object when iam at page2.php...



how can i do this in php????


Regards
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

it's 'I am' not 'iam' and believe it or not you are supposed to have a space after commas.

There is not request object scope built into PHP. You have to append the variables to links like 'http://mysite.com/index.php?myvar=value' to get them to pass to the next page.

You can also make use of sessions to store variables between page requests. Check out the php.net manual.
alaa56mm
Forum Newbie
Posts: 3
Joined: Wed Jul 18, 2007 7:33 am

ok

Post by alaa56mm »

having a space after commas or not. will not resolve the question?
thanks man for your replying..but i dont want to put it in query string like you said...

i believe that there is a solution for this problem
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

$_GET
$_POST
$_SESSION
alaa56mm
Forum Newbie
Posts: 3
Joined: Wed Jul 18, 2007 7:33 am

Post by alaa56mm »

can you kindly give me am example scenario, so i want to see how you store the object and how get it in each page??

regards
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

You can't necessarily store objects, you store strings/variables/arrays.

Page1.php

Code: Select all

session_start();

$_SESSION['Hi'] = 'Hi from page 1';
Page2.php

Code: Select all

session_start();

echo $_SESSION['Hi']; // Hi from page 1;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Objects can be stored in $_SESSION as long as the class definition is present when the session data is unserialized, i.e. before calling session_start().

But the java request object has nothing to do with sessions. It represents data of the current request. If the request is handled the request object is gone, just like $_POST, $_GET and the whole php instance.
If you inlucde/require another php script it is imported into the same php instance and shares the variables of the including script.
You might want to try something like

Code: Select all

$xyz = 'some data';
require 'another_script.php';

Code: Select all

// another_script.php
echo $xyz;
if you want to make sure the variables are in the global scope you can try

Code: Select all

$GLOBALS['xyz'] = 'some data';
require 'another_script.php';

Code: Select all

// another_script.php
echo $GLOBALS['xyz'];
Post Reply