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

Refering to nested object methods.

 
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 -> Feature Requests
View previous topic :: View next topic  
Author Message
satanistlav
Smarty n00b


Joined: 01 May 2007
Posts: 1

PostPosted: Tue May 01, 2007 6:22 pm    Post subject: Refering to nested object methods. Reply with quote

Smarty triggers fatal error when refering to nested object methods like
Code:
{$fooObject->fooMethod()->fooMethod()->fooValue()}
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Tue May 01, 2007 6:56 pm    Post subject: Reply with quote

Yes, it does because it is not a supported feature.
Back to top
View user's profile Send private message
tobias_l
Smarty Rookie


Joined: 11 Aug 2004
Posts: 24

PostPosted: Wed May 02, 2007 2:55 am    Post subject: Reply with quote

what would it take to extend smarty to compile and map such object method calls?
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed May 02, 2007 5:19 am    Post subject: Reply with quote

I seem to recall someone suggesting a patch. Search the forums for "derefrencing methods" or something similar. It wouldn't work for PHP4 without emulation which is bound to be slow and potentially buggy.
Back to top
View user's profile Send private message
tobias_l
Smarty Rookie


Joined: 11 Aug 2004
Posts: 24

PostPosted: Thu May 03, 2007 7:35 pm    Post subject: Reply with quote

thanks for the hint! with php5 an emulation wouldn't be necessary and also not slow, right?
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu May 03, 2007 7:43 pm    Post subject: Reply with quote

Right. With PHP5, it just needs the compiler to observe the syntax in the template and to emit the corresponding PHP5 code. If you've done the search you probably know I don't favour this very much. It is really a programmer's idiom and most people seem to want it to chain get methods on nested objects. This can easily be done under PHP5 using __get(). This means you can do things like $foo->bar->baz in template instead of $foo->getbar()->getbaz() etc. The tempation to pass in "pure" application objects into your templates is greater with method derefrencing and IMHO, that is a very slippery slope.
Back to top
View user's profile Send private message
tobias_l
Smarty Rookie


Joined: 11 Aug 2004
Posts: 24

PostPosted: Fri May 04, 2007 2:51 am    Post subject: Reply with quote

why should the approach using $foo->bar->baz be "better" than using $foo->getbar()->getbaz() ?

my class "foo" (class of object $foo) has the method "bar()" that returns an object. this object does not exists within class "foo" as a property. it gets instanciated in the method and is returned by it.

example:

i have a simple bean like:

class Book {
var $bookId;
var $authorId;
...
/* some getter and setter methods */
...
/* additional methods */
function getAuthor() {
return new Author($this->authorId)
}
}

method "getAuthor" returns an object of type Author that gets instanciated on calling time of method "getAuthor" and not on instanciation time of class Book.
Back to top
View user's profile Send private message
cybot
Smarty Regular


Joined: 20 Apr 2005
Posts: 83

PostPosted: Wed May 16, 2007 9:06 am    Post subject: Reply with quote

tobias_l wrote:
why should the approach using $foo->bar->baz be "better" than using $foo->getbar()->getbaz() ?

my class "foo" (class of object $foo) has the method "bar()" that returns an object. this object does not exists within class "foo" as a property. it gets instanciated in the method and is returned by it.


yes, and exactly for this you could use __get(), as boots mentioned
Back to top
View user's profile Send private message
extra74
Smarty n00b


Joined: 20 Apr 2008
Posts: 3

PostPosted: Sun Apr 20, 2008 2:42 pm    Post subject: Reply with quote

anybody active on this thread?
I have similar problems and I can not solve it...
_________________
www.svensk-server.info
Back to top
View user's profile Send private message Visit poster's website
minichate
Smarty n00b


Joined: 27 Dec 2007
Posts: 1

PostPosted: Wed Apr 23, 2008 2:35 pm    Post subject: Reply with quote

Its a feature I'd really like to have. There is a patch for smarty which does the trick for me but not having it in the core smarty install causes headaches for us.
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 -> Feature Requests 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