View previous topic :: View next topic |
Author |
Message |
satanistlav Smarty n00b
Joined: 01 May 2007 Posts: 1
|
Posted: Tue May 01, 2007 6:22 pm Post subject: Refering to nested object methods. |
|
|
Smarty triggers fatal error when refering to nested object methods like
Code: | {$fooObject->fooMethod()->fooMethod()->fooValue()} |
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue May 01, 2007 6:56 pm Post subject: |
|
|
Yes, it does because it is not a supported feature. |
|
Back to top |
|
tobias_l Smarty Rookie
Joined: 11 Aug 2004 Posts: 24
|
Posted: Wed May 02, 2007 2:55 am Post subject: |
|
|
what would it take to extend smarty to compile and map such object method calls? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed May 02, 2007 5:19 am Post subject: |
|
|
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 |
|
tobias_l Smarty Rookie
Joined: 11 Aug 2004 Posts: 24
|
Posted: Thu May 03, 2007 7:35 pm Post subject: |
|
|
thanks for the hint! with php5 an emulation wouldn't be necessary and also not slow, right? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu May 03, 2007 7:43 pm Post subject: |
|
|
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 |
|
tobias_l Smarty Rookie
Joined: 11 Aug 2004 Posts: 24
|
Posted: Fri May 04, 2007 2:51 am Post subject: |
|
|
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 |
|
cybot Smarty Regular
Joined: 20 Apr 2005 Posts: 83
|
Posted: Wed May 16, 2007 9:06 am Post subject: |
|
|
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 |
|
extra74 Smarty n00b
Joined: 20 Apr 2008 Posts: 3
|
Posted: Sun Apr 20, 2008 2:42 pm Post subject: |
|
|
anybody active on this thread?
I have similar problems and I can not solve it... _________________ www.svensk-server.info |
|
Back to top |
|
minichate Smarty n00b
Joined: 27 Dec 2007 Posts: 1
|
Posted: Wed Apr 23, 2008 2:35 pm Post subject: |
|
|
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 |
|
|