I created a class for managing views and related data. It implements and enables "subpatterns" for easy use as follows: Template
Iterator
ArrayAccess
<p><?php echo $template['foo']; ?></p>
<?php foreach($template->post as $post): ?>
<p><?php echo $post['bar']; ?></p>
<?php endforeach; ?>
In any case, instead of using the built-in basic functions, such as hash()
or date()
, I decided that it would be useful to create a class with a name that would act as a wrapper for any data stored in the templates. TemplateData
Thus, I can add a list of common formatting methods, for example:
echo $template['foo']->asCase('upper');
echo $template['bar']->asDate('H:i:s');
When a value is set through $template['foo'] = 'bar';
in the controllers, the value is 'bar'
stored in its own TemplateData
object.
__toString()
, , - TemplateData
, (string)
. , , , , - :
$template['foo'] = 1;
echo $template['foo'] + 1;
Object of class TemplateData could not be converted to int
; $template['foo']
:
echo ((string) $template['foo']) + 1;
, . - , , , ?