How to Force a Method Call on a Property or Method of an Obj

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gabriel1836
Forum Newbie
Posts: 16
Joined: Sat Jan 03, 2009 12:45 am
Location: Logan, UT

How to Force a Method Call on a Property or Method of an Obj

Post by gabriel1836 »

Here's the problem, I would like to use some form of automatic escaping of html entities. Right now I'm using Zend_View for my templates which means that the templates aren't compiled, and I'm assigning objects to the template and then retrieving values for output from those objects as either properties or methods.

Currently I'm escaping content as it goes into the database (default behavior for Zend_Filter_Input) however this creates issues with updating information because html entities that have been previously escaped as & become & etc. I could fix this but the current solution is flawed in that I really don't want to perform presentational transformations on the data when putting in it in the database, I'd rather do it only when rendering the view.

In my View (using Zend_View so the the view is an object), I make calls to object properties and methods to populate the template like so:

Code: Select all

<?= $this->user->name ?> // Outputs John Doe
<br/>
<?= $this->user->getCompany()->name ?> // Outputs Acme
<br/>
<?= $this->method() ?> // Outputs foobar
If I make it so that all property requests (like for 'user') go through __get() is there any way that I can catch the subsequent calls so that I can force a method call on the final outputted value? For example so that I could do automatic escaping of output.

As I see it right now, I either have to escape the input as it goes into the database or use compiled templates like Smarty does, or switch to assigning every variable to the View object so that it has direct control to force escaping before outputting the data, or use __get() within the model to escape the values if the template is being rendered; none of which is really optimal.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to Force a Method Call on a Property or Method of an Obj

Post by requinix »

gabriel1836 wrote:As I see it right now, I either have to escape the input as it goes into the database
No - databases hold data. When you escape that data you render it useless as the original data it was.
gabriel1836 wrote:or use compiled templates like Smarty does
Okay. In fact, if I remember correctly, the ZF documentation gives you an example of how to do that.
gabriel1836 wrote:or switch to assigning every variable to the View object so that it has direct control to force escaping before outputting the data
"Correct" solution.
gabriel1836 wrote:or use __get() within the model to escape the values if the template is being rendered
No. Handling the View in the Model is a blatant violation of MVC, which (since you're using ZF) you should observe.
Post Reply