• Packages
  • Themes
  • Documentation
  • Blog
  • Discussions
Sign in

php-getters-setters

generate getters and setters for your php class
francodacosta
25,976
26
  • Repo
  • Bugs
  • Versions
  • License
Flag as spam or malicious

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:

  • Generate Getters, Setters or Both
  • Select all variables or just some via UI
  • Control method scope via a DocBlock tag
  • intelligent guessing of variable names, if you use descriptive variable names you do not need to provide a description, the method comment will set accordingly
  • supports _ in property names

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

I think this package is bad news.

Good catch. Let us know what about this package looks wrong to you, and we'll investigate right away.

  • Terms of Use
  • Privacy
  • Code of Conduct
  • Releases
  • FAQ
  • Contact
with by