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

calling accessor functions to PHP objects from Smarty?

 
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 -> General
View previous topic :: View next topic  
Author Message
apinstein
Smarty n00b


Joined: 16 Jun 2003
Posts: 3

PostPosted: Mon Jun 16, 2003 9:32 pm    Post subject: calling accessor functions to PHP objects from Smarty? Reply with quote

I have just started using Smarty and have a question.. I try to implement OO best-practices in my coding. That means usings objects with private members and accessing data through $obj>get_*() and $obj->set_*() methods.

Unfortunately, smarty seems to have only the ability to access object data, but not call object accessors like so:

$smarty->assign("user_obj", $user);

---
Your name is: {$user_obj->name}
Your email is: {$user_obj->email}

However, I would rather do:

Your name is: {$user_obj->get_name()}
Your email is: {$user_obj->get_email()}

==========
Is this possible? Is this planned? Or is the best-practice to assing ALL variables to smarty and just not use objects:

$smarty->assign('user_name', $user->get_name());
$smarty->assign('user_email', $user->get_email());


Thanks in advance,
Alan
Back to top
View user's profile Send private message
mischa
Smarty Rookie


Joined: 30 Apr 2003
Posts: 19
Location: Suhr, Switzerland

PostPosted: Mon Jun 16, 2003 11:13 pm    Post subject: Reply with quote

Hi Alan

Yes, this is possible. I'm using Smarty-2.5.0 and {$object->setName($name)} or {$obj->getName()} works well!
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Jun 17, 2003 12:04 am    Post subject: Reply with quote

If you check the docs, you see that you can also register objects so that they have a "nicer" syntax. If you don't use register_object then you can use assign as you have done or the assign_by_ref, as appropriate. Otherwise, what you suggest alread works fine.
Back to top
View user's profile Send private message
apinstein
Smarty n00b


Joined: 16 Jun 2003
Posts: 3

PostPosted: Tue Jun 17, 2003 3:04 am    Post subject: Reply with quote

awesome. Thanks for the response.

I actually never TRIED it since it wasn't documented in the manual as doable... figured that they'd mention it if it were in the section right next to objects.

Cool!
Alan
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 -> General 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