php-getters-setters

generate getters and setters for your php class

francodacosta

26,007

26

0.5.9

MIT

GitHub

PHP Getters and Setters

With PHP Getters and Setters you can automatically generate Getters and Setters for your php classes.

The code produced is PSR compatible

Features:

Example PHP Code

class test
{
    /**
     * foo container
     *
     * @var AbcClass
     */
    private $foo;
}

Example class after generating Getters and Setters

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
     *
     * @return self
     */
    public 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!

Special DocBlock tags

@internal: getter and setter will be private

@private: getter and setter will be private

@protected: getter and setter will be protected

@read-only private|protected: getter will be public, setter will be private or protected (defaults to private)

Settings:

doNotTypeHint: an array of items that when present in @type or @var declarations are ignored and not used as type hint

camelCasedMethodNames: method names will follow PSR rules PSR states that all method names must be camel cased, if set to false method names won't be Camel Cased

getterTemplate: the template for the getter

setterTemplate: the template for the setter

Default templates

A rudimentary template editor is available at Packages -> PHP Getters and Setters -> Template Editor

Getter

\ \ \ \ /**\n
\ \ \ \ * Get the value of %description% \n
\ \ \ \ * \n
\ \ \ \ * @return %type%\n
\ \ \ \ */\n
\ \ \ %scope% function %methodName%()\n
\ \ \ {\n
\ \ \ \ \ \ \ return $this->%variable%;\n
\ \ \ }\n
\n

Setter

\ \ \ \ /** \n
\ \ \ \ * Set the value of %description% \n
\ \ \ \ * \n
\ \ \ \ * @param %type% %variable%\n
\ \ \ \ * \n
\ \ \ \ * @return self\n
\ \ \ \ */\n
\ \ \ %scope% function %methodName%(%typeHint%$%variable%)\n
\ \ \ {\n
\ \ \ \ \ \ \ $this->%variable% = $%variable%;\n
\n
\ \ \ \ \ \ \ return $this;\n
\ \ \ }\n
\n