Several GenHelm models support the generation of PHP classes for which custom code can be added to the generated class components. This includes the models:

  • php_class
  • custom
  • model
  • form_handler

If you are considering developing a new model that will generate specialized PHP classes you may want to take advantage of the base functionality handled by the spec object. These modes all make use of two flexgrids which allow users to define class properties and methods.

model_php_class_variables

This flexgrid enables users to define class properties and optionally generate default get and set methods for each property.

custom_code_php_class

This flexgrid allows users to enter class methods and supplement methods generated by the model.

Preparing Custom Code

The following code can be used to check whether custom code has been entered and to prepare the code for processing.

$custom = $this->get_spec_row('custom_code_php_class');
if (empty($this->spec_row['custom_code_php_class'])) {
    $custom_code = false; // No custom code was entered
}
else {
    $custom_code = $this->configure_php_class_custom_code($this->spec_row['custom_code_php_class'],'custom'); 
}

The inherited method configure_php_class_custom_code is passed the name of the custom code flexgrid as well as a module identifier. This module identifier is normally the same as your model name. It returns a multi-dimensional array containing custom code entered by the user.

get_php_custom_code

Inherited method get_php_custom_code is used to assemble custom code in order to generate class methods. Here we see the signature of  this function:

function get_php_custom_code($module_id,$section_type,$section_id,$location,&$custom_code,$options=array())

The first three parameters are used to uniquely identify a row within the custom code grid.

Let's review the parameters that are passed to this function.


module_id
This must be the same value passed as the module identifier in parameter 2 of the call to configure_php_class_custom_code.
section_type

This parameter corresponds to the Section Type parameter selected in the custom code flexgrid. This value must be either property, function, get or set.

Section Type Selector

section_id

The section_id parameter corresponds to the next column of the flexgrid. For get, set and property section types this value refers to a class variable (property) name. For function section types this property corresponds to a function name.

Custom code section id

location

The location value corresponds to the next column wherein the user can select the location in which the code is to be placed.

Location selector in custom code flexgrid

Instead of the text values, the function uses the following location identifiers:

*Replace Section
{} Replace Body
{Top of Body
}Bottom of Body
+After Last Sibling
custom_code 
This is the array returned by the configure_php_class_custom_code method.
options
Options are used to configure the generated code.

Generating Properties

The following code shows how class property definitions can be generated included default get and set methods. This code also checks the custom code flexgrid tO see whether get and set methods have been overridden or supplemented and the get and set methods are adjusted accordingly.

$variables = $this->get_spec_row('model_php_class_variables');
$get = '';
$set = '';
if ($variables !== false) {
    $location = '';
    foreach ($variables as $field_name => $cols) {
      	// Override property definitions found within the variable grid
        $code .= $this->get_php_custom_code('custom','property',$field_name,$location,$custom_code,$cols);
        if ($cols['get']) {
            $get .= $this->get_php_custom_code('custom','get',$field_name,$location,$custom_code,$cols);
        }
        if ($cols['set']) {
            $set .= $this->get_php_custom_code('custom','set',$field_name,$location,$custom_code,$cols);
        }
    }
}
// Check for new properties only coded within the custom code grid
$code .= $this->get_php_custom_code('php_class','property','','',$custom_code);
$code .= $get.$set;

Mandating Required Methods

One of the features of the custom code handing mechanism is that you, as a model developer, can require the users of your model to code one or more mandatory methods. We have seen this feature in the custom model where users are required to code a generate method. Here we see the code sequence that handles this.

$required_code = $this->get_php_custom_code('custom','function','generate','{}',$custom_code,array('required_section'=>true));
if ($required_code === false) {
    return false; // Message set by base class
}
$code .= $required_code;

Notice that we have passed the option required_section which is assigned the value of true. Hence, if the method does not find a matching section an error will be raised. 

Optional Method Supplementing or Replacing

You can also supplement of replace certain functions. Recall that the model model generates a function named clear_spec_details automatically. Nevertheless, the model also allows users to add code before or after the default code or they can also replace the default code entirely with custom code. Here we see how this can be achieved.  Before this code, the variable $clear contains the default clear_spec_details code body.

  $code .= $this->get_php_custom_code('model','function','clear_spec_details','',$custom_code,
                                      array('required_section'=>false,'code'=>$clear));

In this case, since required_code is set to false, the default code will be used directly if the user has not provided code to supplement or replace the default code. Otherwise, the user supplied custom code will supplement or replace the default code, depending on the location setting.

Requesting the Custom Code Functions

In the final example we show how you can call the inherited get_php_custom_code method to request all of the miscellaneous functions entered by the user.

$code .= $this->get_php_custom_code('model_class','function','','+',$custom_code);
Specification Used by the Model Model
🡇
Specification Used by the Model Model