Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Assign a value to an object field convert it to an array

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
riquito
Smarty n00b


Joined: 30 Nov 2010
Posts: 3

PostPosted: Tue Feb 08, 2011 11:16 am    Post subject: Assign a value to an object field convert it to an array Reply with quote

Steps to reproduce it

Code:

class Example {
   public $field="foo";
}

1) assign an instance of Example to a variable and pass it to a template
2) {print_r($object)}
Example Object (
    [field] => "foo"
)

3) {$object.field="bar"}
4) {print_r($object)}
Array (
    [field] => "bar"
)


Now, this was (to me) completely unexpected.

The documentation refers to the dot assignment in an example about arrays
Quote:

Example 7.3. Assignment of an array element

{$user.name="Bob"}



I'd expect that using that syntax on an object I'd get either an exception (not an array or similar) or a correct assignment to the field. Having the object transformed in an array under the hood can be pretty difficult to spot (smarty often lets you use them in the same ways but an object is a completely different beast, and maybe you want to pass it to filters that expect an object).

If this is considerad a "feature" the documentation should inform the user somewhere (or does it ?)
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Feb 08, 2011 3:56 pm    Post subject: Reply with quote

It's not possible to assign object properties from within the template directly.

The only way I currently see is to implement an object method for this job.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs 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