View previous topic :: View next topic |
Author |
Message |
cybot Smarty Regular
Joined: 20 Apr 2005 Posts: 83
|
Posted: Wed May 16, 2007 9:15 am Post subject: silently ignoring method calls on not existing objects |
|
|
Hi,
Smarty silently ignores variables not set - why not the same for methods on not set objects?
Code: | -{$some_notexistant_var}- |
produces:
but
Code: | {$not_existing_object->method()} |
produces error, why not like the above just an empty string?
I know, in PHP a not existing var is evaluated to on empty string if printed, and for every var called a name entry is created in the var lookup tables with no content, means NULL, ... and all the other things PHP does and not does with vars and method calling on non-objects ...
But from the point of view of a designer this is not consistent.
And as Smarty is just a wrapper/mapper for PHP it should handle this consistently.
What do you think? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed May 16, 2007 2:50 pm Post subject: |
|
|
Smarty won't handle that for you, a method isn't treated like a variable. If you are using PHP5, you can do method overloading with __call(). |
|
Back to top |
|
cybot Smarty Regular
Joined: 20 Apr 2005 Posts: 83
|
Posted: Wed May 16, 2007 3:24 pm Post subject: |
|
|
mohrt wrote: | Smarty won't handle that for you |
Smarty also handles foreach on non-arrays
mohrt wrote: | a method isn't treated like a variable. |
i know the differences between methods and variables - this was not what i was talking about
mohrt wrote: | If you are using PHP5, you can do method overloading with __call(). |
the problem is not the call but the not existing object |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed May 16, 2007 3:41 pm Post subject: |
|
|
I see, I thought you mean a non-existing method of an object.
So, what happens in PHP when you reference a non-existing variable? A notice. What happens when you reference a method of non-existing object? A fatal error. Smarty carries this behavior though since the variables are just compiled into PHP code.
Also, silently ignoring non-existant objects would make debugging more difficult. Is there a good reason why you want to be able to reference non-existant objects in the template? |
|
Back to top |
|
cybot Smarty Regular
Joined: 20 Apr 2005 Posts: 83
|
Posted: Wed May 16, 2007 5:24 pm Post subject: |
|
|
mohrt wrote: | So, what happens in PHP when you reference a non-existing variable? A notice. What happens when you reference a method of non-existing object? A fatal error. Smarty carries this behavior though since the variables are just compiled into PHP code. |
the question is not what happens in PHP, Smarty is not PHP (as PHP is not C even if written C)
mohrt wrote: | Also, silently ignoring non-existant objects would make debugging more difficult. |
why?
and Smarty also hides Notices of not defined Variables - this makes debugging not easier ...
mohrt wrote: | Is there a good reason why you want to be able to reference non-existant objects in the template? |
because it is a template, and a template designer does know nothing about PHP in worst case!
because it is this what someone would expect, show nothing if it does not exist
like it is already for scalar variables |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed May 16, 2007 7:22 pm Post subject: |
|
|
IMHO, this is one of the problems that arise when people want to pack objects into templates and use them for more than simple data containers. Templates already have defined and controlled behaviors given by the plugin/tag system. Objects introduce a new layer into which behavior can be introduced but it does so without any oversight since that layer necessarily introduces a PHP idiom.
Anyone would expect a method to be executed if it is issued in the template. Similarly, anyone would expect a Smarty tag to either be executed or to fail in a fatal way if it did not exist. Saying otherwise by giving variable access as a comparison is somewhat disingenuous.
It is up to you to provide the appropriate response from your object -- for example, you could wrap it with a proxy object that passes through valid calls and ignores invalid calls (using __call as Monte suggested). Otherwise, you might be better off forgoing the use of objects if you expect them to get abused in your templates. |
|
Back to top |
|
cybot Smarty Regular
Joined: 20 Apr 2005 Posts: 83
|
Posted: Wed May 16, 2007 8:15 pm Post subject: |
|
|
boots wrote: | IMHO, this is one of the problems that arise when people want to pack objects into templates and use them for more than simple data containers. |
did i write that my object is more than simple data container? and even if so - what is the difference between 'simple data container' and ... 'normal objects'?
boots wrote: | Templates already have defined and controlled behaviors given by the plugin/tag system. Objects introduce a new layer into which behavior can be introduced but it does so without any oversight since that layer necessarily introduces a PHP idiom. |
this is not relevant, IMHO, for a template designer it makes no difference if he uses $var, $array.key or $object->method() - it is all the same: a placeholder.
boots wrote: | Anyone would expect a method to be executed if it is issued in the template. Similarly, anyone would expect a Smarty tag to either be executed or to fail in a fatal way if it did not exist. Saying otherwise by giving variable access as a comparison is somewhat disingenuous. |
not 'Anyone' - really not!
Fatal errors should NEVER occur, it is a fault of the application/library if they occur - in this case the 'application' is Smarty - errors should be handled - even if the application/library is provided wrong data!
Smarty should display an empty string if a variable is not set - and should not try to call something on something not existent - and in debug mode you would see that Smarty tells you the variable does not exist ... |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed May 16, 2007 8:33 pm Post subject: |
|
|
Having methods on the object make it more than a simple data container. Objects encapsulate two distinct but related items: data and behaviour. Your proposal seems to be keying on the behavior side of the coin and I am arguing that introducing behavior via objects is in-itself a slippery (and perhaps undefined) slope.
As for the placeholder analogy, I disagree. You are saying that $object->method() is the same as a variable access and I am claiming it is the same as writing {method}. {$variable} is expected to be silent. {method} is not.
If you want to avoid fatal errors, be sure to color within the lines. Don't pass in objects that can break
BTW, I don't think you are fully appreciating what implementing this entails; in short, it introduces an enormous overhead where Smarty would, for each invocation of the template and on each reference in the template to a method call, use is_callable(). Imagine that in a loop of results. What is the justification for making well-behaved templates incur such overheads? |
|
Back to top |
|
cybot Smarty Regular
Joined: 20 Apr 2005 Posts: 83
|
Posted: Wed May 16, 2007 8:57 pm Post subject: |
|
|
you always speak in technical terms, or from the point of view of a developer!
take a step back!
do you think it is right that my Word Processor always crashes/closes if i supply wrong data/document?
i think not
Quote: | Don't pass in objects that can break |
the fatal errors arise not from assigning objects to Smarty - they arise if the template designer writes {$foo->bar()} ... so you think i should check every template before saving for invalid method calls? or ANYTHING a template designer could do wrong ... it is not only invalid method calls, it is all a designer could do wrong - and he does not understand PHP error messages - a windows user does not understand low level file system errors - thats why the application wrap them and display an error message comprehensible respecting the level of knowledge of the user
Quote: | BTW, I don't think you are fully appreciating what implementing this entails; in short, it introduces an enormous overhead where Smarty would, for each invocation of the template and on each reference in the template to a method call, use is_callable(). Imagine that in a loop of results. What is the justification for making well-behaved templates incur such overheads? |
i would write in assembler to avoid overhead ... |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu May 17, 2007 3:02 am Post subject: |
|
|
cybot wrote: | i would write in assembler to avoid overhead ... |
LOL ok time to stop feeding the troll |
|
Back to top |
|
cybot Smarty Regular
Joined: 20 Apr 2005 Posts: 83
|
Posted: Thu May 17, 2007 5:09 am Post subject: |
|
|
mohrt wrote: | cybot wrote: | i would write in assembler to avoid overhead ... |
LOL ok time to stop feeding the troll |
of course - this was a joke ... but i don't feel happy called a troll ...
i understand that this is somehow a fundamental question for an app overall, and Smarty in specific - it is just that i feel Smarty is inconsistent in this way ...
Do you not think that is ok if an application/library throws fatal errors if it could catch them? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu May 17, 2007 5:20 am Post subject: |
|
|
If a designer puts {soidfrusdif} in a template, its an error. They have to fix it. If they put {$*&%$%} its an error, fix it. Likewise, if they put {$foo->bar()} and no object exists, fix it. So, stop arguing just for arguments sake. There is no practical purpose to silently ignore a non-existing object, besides causing confusion when a typo happens. Fix the error,move on. |
|
Back to top |
|
cybot Smarty Regular
Joined: 20 Apr 2005 Posts: 83
|
Posted: Thu May 17, 2007 5:28 am Post subject: |
|
|
sorry for arguing ... but you are unfair!
i have no problem if Smarty developers say it is this way, intentionally, and will stay this way. ok then!
i understand and respect this
i just argued because you did not say that before, you argued with the behavior of PHP, it seems you did not understand my intention on the first time.
i have no time to "arguing just for arguments sake" ... |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu May 17, 2007 5:55 am Post subject: |
|
|
fair enough, hope we've got this sorted out then. |
|
Back to top |
|
|