- 3.5. Expressions
3.5.6. Objects - 3.5.5. Backticks
« Previous - 3.5.7. HTML escaping
Next »
3.5.6. Objects
This page describes a feature strictly related to PHP language. We do not recommend to use it in your projects unless absolutely necessary, as it reduces the portability and links the templates with a concrete script implementation.
Open Power Template expression syntax can optionally support PHP objects directly from the template side.
Supported OOP features
- Accessing object fields.
- Accessing object methods.
- Accessing static fields and methods of the registered classes.
- Creating new objects of registered classes (advanced use).
- Cloning the objects (advanced use).
Enabling or disabling object support
This feature is strictly related to the PHP language which may lead to the problems with portability, refactoring and security. This is why it may be disabled in some scripts or even not supported. In order to enable the OOP on the template side, set the following configuration options to true:
$tpl = new Opt_Class; // ... $tpl->basicOOP = true; $tpl->advancedOOP = true; // ... $tpl->setup();
Moreover, if you are going to create new objects or accessing the static class members, you have to register the appropriate classes in the template engine:
$tpl->register(Opt_Class::PHP_CLASS, 'templateClassName', 'realPHPClassName');
OPT does not support PHP namespaces on the template side. However, you can specify the class namespace in the
register()method.
Accessing object members
OPT provides only one object access operator: ::. Depending on the context, it may refer either to the static or normal members:
$object::field - accessing object field
$object::method() - accessing object method
className::field - accessing static class field
className::method() - accessing static class method
The complex calls are also possible: className::method()::field::submethod().
Creating new objects
If the advancedOOP option is enabled, OPT allows you to create new objects of registered classes:
$object is new className
$object is new className('constructor arguments')
With this option, you may also clone existing objects:
$objectA is clone $objectB
Why should you not use objects in templates?
You could have used to use objects with pure PHP or other template engine, but there you had no other choice! The reasons why you should not use them in templates are:
They require you to know the exact script implementation while writing the templates. It decreases the refactoring possibilities. Let's say that your template contains a text
$item::field. It would not work without an object here and moreover, if you decide to make this field private and add the accessor method, you must modify the template, too.They can transparently move the application logic to the view layer. The view layer should contain the presentation logic only and the experiences with other template engines show that some programmers attempt to call the SQL queries or database models directly from the templates. This is not what the templates are supposed to do and believe me - in 99% cases you are wrong saying that you really need those objects in them.
OPT provides you several features that hide the implementation details from you.
The OPT techniques to replace objects
- Containers - they are generic compound data structures with syntax
$item.subitem.subsubitem. With data formats the PHP script may replace them to any stuff you need: arrays, objects, some script-specific structures etc. - Components - useful for dealing with HTML forms.
- Blocks - like above, but more generic.
- OPT functions - you may register static class methods as OPT functions.
- 3.5.6. Objects
3.5. Expressions - « Previous
3.5.5. Backticks - Next »
3.5.7. HTML escaping