Re: [SMARTY-DEV] objects: one more approach

From: Date: Fri, 17 Jan 2003 20:13:35 +0000
Subject: Re: [SMARTY-DEV] objects: one more approach
References: 1  Groups: php.smarty.dev 
Request: Send a blank email to php-smarty-dev+get-562@lists.php.net to get a copy of this message
On Wed, Jan 15, 2003 at 10:15:45AM -0600, Monte Ohrt wrote: > How about this. > > You assign objects just like variables. Let's say $foo is an assigned > object. > > At first I thought about this syntax: > > {$foo.bar} <-- property > {$foo.bar p1="foo" p2=$bar} <-- method > > But, this isn't simple because at compile time the parser doesn't know > if $foo.bar is an array or an object, they require different syntax in > the compiled template. I could either pass this to a function and figure > it out at runtime (extra step), or use a separate syntax for objects: > > {$foo->bar} <-- property > {$foo->bar p1="foo" p2=$bar} <-- method I think it's worth passing $foo.bar to a function and using 'is_object()' and 'method_exists()' to resolve any ambiguity between arrays/objects and properties/methods. This way the template author doesn't have to learn any new syntax (->), or even know what an object is, which is good. Then you can treat an array of objects in a template the way you would an array of associative arrays (like $rows from a db -- $r.name or $o.name, the template designer doesn't need to know where it's coming from). > ok, now we know at runtime if it is an object, and how to compile it. > Also with this syntax of passing parameters it is nothing new for the > parser. The one problem I see is no distiction between a property and a > method with no args. Ideas? Maybe we don't worry about it. If the method > exists, use that. Otherwise use the property. > > If an object is not registered, you have access to all methods and > properties except private ones. If registered, then restricted access to > registered methods (but leave access to properties, or register them > too?) > > I'm also wondering how the object parameters should be handled. example: > > {$foo->bar p1="foo" p2=$bar} > > which should happen? > > $foo->bar("foo", $bar); > > or > > $foo->bar(array('p1' => "foo", 'p2' => $bar), $this); > > Notice the second one is much like registered functions, all parameters > are passed as an array in the first argument, and the smarty object > passed in the second. Maybe we should do this with objects too. > I think the first option. If you need the smarty object, just register a wrapper function. Existing classes that don't know about smarty are more likely to want '$foo->bar("foo", $bar)' and won't know what to do with $this. - Rob

« previous php.smarty.dev (#562) next »