[PhpStorm] Create getter setter


Writing time : 2017-11-24 01:43:50

Create getter setter

Creating a getter setter


Declaring a member variable in the class, placing the cursor on the declared variable, selecting the Generate menu of the Code menu or pressing the shortcut key Alt + Insert, the Generate pop-up appears.

If you select the Getters and Setters.. item in the pop-up, the Choose Fields pop-up appears. Select all variables to add getter setters and click the OK button to automatically add a function to the editor.

Modify getter setter format


The getter setter is added in camel notation, but if you change the setting, it can be changed to the underbar format.

To change to the underbar format, select File and Code Templates in Settings and PHP Getter in the Code tab.

Modify getter format


In the edit window, change the function ${GET_OR_IS}${NAME} to function ${GET_OR_IS}_${FIELD_NAME}.

Before change

/**  
* @return ${TYPE_HINT}  
*/  
public ${STATIC} function ${GET_OR_IS}${NAME}()#if(${RETURN_TYPE}): ${RETURN_TYPE}#else#end  
{  
#if (${STATIC} == "static")  
return self::$${FIELD_NAME};  
#else  
return $this->${FIELD_NAME};  
#end  
}  

after change

/**  
* @return ${TYPE_HINT}  
*/  
public ${STATIC} function ${GET_OR_IS}_${FIELD_NAME}()#if(${RETURN_TYPE}): ${RETURN_TYPE}#else#end  
{  
#if (${STATIC} == "static")  
return self::$${FIELD_NAME};  
#else  
return $this->${FIELD_NAME};  
#end  
}  

setter format modification


In the same way, select PHP Setter Method in the Code tab.

Change the function set${NAME} part to function set_${FIELD_NAME}.

Before change

/**  
* @param ${TYPE_HINT} $${PARAM_NAME}  
*/  
public ${STATIC} function set${NAME}(#if (${SCALAR_TYPE_HINT})${SCALAR_TYPE_HINT} #else#end$${PARAM_NAME})  
{  
#if (${STATIC} == "static")  
self::$${FIELD_NAME} = $${PARAM_NAME};  
#else  
$this->${FIELD_NAME} = $${PARAM_NAME};  
#end  
}  

after change

/**  
* @param ${TYPE_HINT} $${PARAM_NAME}  
*/  
public ${STATIC} function set_${FIELD_NAME}(#if (${SCALAR_TYPE_HINT})${SCALAR_TYPE_HINT} #else#end$${PARAM_NAME})  
{  
#if (${STATIC} == "static")  
self::$${FIELD_NAME} = $${PARAM_NAME};  
#else  
$this->${FIELD_NAME} = $${PARAM_NAME};  
#end  
}  

Click OK to save the settings.

Reference URL


https://stackoverflow.com/questions/6768793/get-the-full-url-in-php

Previous post

Next post

Other posts in the category