With PHP Getters and Setters you can automatically generate Getters and Setters for your php classes.
Generate PHP code
class test
{
/**
* foo container
*
* @var AbcClass
*/
private $foo;
}
Go to Tools -> PHP Getters and Setter
Getter and Setter will be generated:
class test
{
/**
* foo container
*
* @var AbcClass
*/
private $foo;
/**
* Gets the foo container.
*
* @return AbcClass
*/
public function getFoo()
{
return $this->foo;
}
/**
* Sets the foo container.
*
* @param AbcClass $foo the foo
*/
private function _setFoo(AbcClass $foo)
{
$this->foo = $foo;
return $this;
}
}
As you can see if get to trouble of commenting your variables, the generated functions can be used without modification.
This is an huge time saver!
Commands available are:
These can be accesed via the context menu (right click on the source of any open PHP file) or the command pallette. The currently open file must be a PHP file.
type : boolean
default : false
description: ignore visibilty for setters generation
type : array
default: []
description: the user templates to load
type : string
default: camelCaseFluent
description: the template to use
type: list of strings
default: ["mixed", "int","integer", "double", "float", "number", "string", "boolean", "bool", "numeric", "unknown"]
description: if the property has one of the types listed type hinting will not be used
type: boolean
default: false
description: Set to true to generate setter code before getters
[package-dir] is your package directory.
[package-dir]/PHP Getters and Setters
.Put the following in a file at [package-dir]/PHP Getters and Setters/user_templates.py
.
class myTemplate(object):
name = "myTemplate"
style = 'camelCase' # can also be snakeCase
getter = """
/**
* Gets the %(description)s.
*
* @return %(type)s
*/
public function get%(normalizedName)s()
{
return $this->%(name)s;
}
"""
setter = """
/**
* Sets the %(description)s.
*
* @param %(type)s $%(name)s the %(humanName)s
*
* @return self
*/
public function set%(normalizedName)s(%(typeHint)s $%(name)s)
{
$this->%(name)s = $%(name)s;
}
"""
Preferences | Package Settings | PHP Getters and Setters | Settings - User
.Add the following settings
// user defined templates to load
"registerTemplates" : [ "myTemplate" ],
// the template used to generate code
"template" : "myTemplate",