Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Object in session fails

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
ZakMc
Smarty Pro


Joined: 04 Oct 2004
Posts: 182

PostPosted: Fri Nov 12, 2010 1:40 pm    Post subject: Object in session fails Reply with quote

I have the problem that this code dont work anymore

Code:
{$smarty.session.user->getId()}


Quote:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in ...


I take a look in the templates_c file

Code:
echo (isset($_SESSION['user'])? $_SESSION['user'] : null)->getId();


I think the correct code is

Code:
echo (isset($_SESSION['user'])? $_SESSION['user']->getId() : null)


In RC4 it works fine.

I look at the templates_c file created with RC4 there is no isset

Code:
$_SESSION['user']->getId()


I found in this thread what was change

http://www.smarty.net/forums/viewtopic.php?t=18172

now smarty check with isset to prevent errors, but handle with objects in session fails
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Nov 12, 2010 2:08 pm    Post subject: Reply with quote

Yes, you are right. There is no quick fix for object in sessions.

Currently there is only the workaround:
Code:
{$foo = $smarty.session.user}
{$foo->getId()}
Back to top
View user's profile Send private message
ZakMc
Smarty Pro


Joined: 04 Oct 2004
Posts: 182

PostPosted: Mon Nov 15, 2010 8:22 am    Post subject: Reply with quote

we had build our templates with that. Now i must change about 300 templates.
I think that's a bad change from RC4 to Release. In RC version i dont expected so serious changes.

It is so rare that someone use objects in sessions?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Nov 15, 2010 3:03 pm    Post subject: Reply with quote

Uwe is working on a fix. However, a better work around (and generally preferred solution):

Code:
$smarty->assign('myobj',$_SESSION['myobj']);


Code:
{$myobj->foo()}


As the documents state:

http://www.smarty.net/docs/en/language.variables.smarty

Code:
Although Smarty provides direct access to PHP super globals for convenience, it should be used with caution. Specifically, GET and POST and REQUEST are commonly used for presentation purposes, but directly accessing SERVER, ENV, COOKIE and SESSION vars is typically avoided, as this is mixing underlying application code structure into the templates. A good practice is to assign specific needed values to template vars.


It is better to limit the assign to the specific object you want, not the whole PHP session. And it is not so common for objects to reside in SESSIONS, as they get serialized/deserialized, and that can have unwanted consequences.

That said, the parser should be able to handle what you are trying to do, and I think Uwe is looking at a way to resolve it. This problem cropped up when we removed all E_NOTICE situations from templates, and removed the necessity of applying the E_NOTICE mask to all templates/plugins.
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Mon Nov 15, 2010 7:18 pm    Post subject: Reply with quote

A fix for using session vars as object is in the SVN now
Back to top
View user's profile Send private message
ZakMc
Smarty Pro


Joined: 04 Oct 2004
Posts: 182

PostPosted: Tue Nov 16, 2010 10:08 am    Post subject: Reply with quote

@mohrt
Thx for explaining.
We save the user object in the session because we use the actuall user object in many subtemplates. We think it is better to save it in session so we dont need to build the object on every page. Also we check if the user object is in session so we know he is logged in. Until now we have no problems with
serialized/deserialized objects in sessions.

In future we will use a global template var for it, but that is many work for us Smile

@tews
thx for the fix
Back to top
View user's profile Send private message
IanCar134
Smarty n00b


Joined: 15 Nov 2010
Posts: 1

PostPosted: Tue Nov 16, 2010 12:39 pm    Post subject: Reply with quote

Thanks also for explanation and fix.

We also have a user object in the session in the same way and would ideally like to keep things like that if possible.

Great work - many thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3 All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP