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:
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.
This flexgrid enables users to define class properties and optionally generate default get and set methods for each property.
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.
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.
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:
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.
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.
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.
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.
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.