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

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:

The OPT techniques to replace objects

3.5.6. Objects
3.5. Expressions
« Previous
3.5.5. Backticks
Next »
3.5.7. HTML escaping