The php_trait model works in conjunction with the php_class model and is used to generate traits that can be used by various classes. Traits are collections of properties and methods that can be used by any number of classes. Unlike inheritance, which only allows one parent class, a class can use any number of trait definitions.

Suppose you have a collection of classes that are helpful for generating HTML. These could be combined into an "html_gen" trait definition. Furthermore, if you have some functions that are useful in generating JavaScript, these could be combined into a "script_gen" trait. If you are developing a class that needs to generate both HTML and JavaScript you could include use statements to incorporate the html_gen and script_gen traits.

Defining traits is very similar to creating classes. Therefore, to learn how to define properties and methods within your trait definitions please review the php_class help.

Using Traits

Including the trait Definition

Traits defined using the php_trait model are generated into the trait subfolder of the main classes folder. Therefore, to incorporate traits defined within system you first have to require the trait using:

require_once CLASS_PATH.'trait/tname.php';

If the trait is defined within the current site, it should be included using:

require_once SITE_CLASS_PATH.'trait/tname.php';

In each case, tname is the name of the trait to be used.

The reference should be defined within the Before Class definition as shown.

Requiring a PHP Trait Definition

Adding the use Statement

Within the Custom Code section, set the Section Type to be use and set the Location to be After Last Sibling. Enter the full use statement within the Custom Code column as shown:

Using PHP traits

Sorting Properties and Functions

You can sort both properties and functions (methods) by using the sort command. To only sort properties you can enter the command sort properties. To only sort functions, enter the command sort functions.

Sample php_trait definition
🡇
Sample php_trait definition