Generating Getters and Setters

Zend Studio can automatically create getter and setter functions in order for 'Get' and 'Set' function calls to be easily created.

This procedure describes how to generate getter and setter functions for all variables within a class.



Instructions on how to complete a procedure

To generate getters and setters:

  1. In a PHP file, place your cursor within the class for which you would like to generate the getters and setters.

  2. Right-click within the class's source code and select Source | Generate Getters and Setters -Or- from the Menu Bar go to Source | Generate Getters and Setters
    -Or- Right-click the required class in PHP Explorer view and select Source | Generate Getters and Setters

  1. The 'Generate Getters and Setters' dialog will open, displaying all variables that might require a getter/setter.

Generate getters and Setters dialog

  1. Select:

  • Which variables getter and setter functions should be created for.
    Expand the list under each function by clicking the + icon in order to select to generate only a getter or a setter for each variable.
    If your cursor was originally placed on a certain variable, this will automatically be selected.

  • Insertion Point - Select the location in the class where you want the entries to be added from the drop-down list.
    Options are:

    • First method - Getters and setters will be placed as the first methods within the class.

    • Last method - Getters and setters will be placed as the last methods within the class.

    • Cursor position - Getters and setters will be placed at the cursor position  (only available if cursor was placed in a valid position).

    • After function ... - Getters and setters will be placed after the selected function (depending on the functions available within the class).

  • Sort by - Determine the order in which the entries are entered.
    The options are:

    • First getters, then setters - All the getters will be grouped together, followed by the setters.

    • Fields in getter/setter pairs - Pairs of getters and setters relating to the same variable will be generated together.

  • Access modifier (Not available in PHP 4 projects) - Selects whether the functions will be public, protected, private or final.

  • Generate method comments - Select whether to generate a PHP Docblock for each entry.

  1. Click OK.

The relevant getter and setter functions will be generated for the selected functions.

Generated getter and Setter