ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

PHP Getters and Setters

generare getters and setters for php classes

Details

  • 3 2.0.15
    1.0.10
  • github.​com
  • github.​com
  • 6 months ago
  • 1 hour ago
  • 5 years ago

Installs

  • Total 35K
  • Win 17K
  • OS X 9K
  • Linux 9K
Dec 12 Dec 11 Dec 10 Dec 9 Dec 8 Dec 7 Dec 6 Dec 5 Dec 4 Dec 3 Dec 2 Dec 1 Nov 30 Nov 29 Nov 28 Nov 27 Nov 26 Nov 25 Nov 24 Nov 23 Nov 22 Nov 21 Nov 20 Nov 19 Nov 18 Nov 17 Nov 16 Nov 15 Nov 14 Nov 13 Nov 12 Nov 11 Nov 10 Nov 9 Nov 8 Nov 7 Nov 6 Nov 5 Nov 4 Nov 3 Nov 2 Nov 1 Oct 31 Oct 30 Oct 29 Oct 28
Windows 13 19 10 12 14 17 22 13 18 7 8 17 13 18 14 17 12 12 14 26 17 33 19 14 12 12 28 16 11 12 7 7 15 20 16 21 9 5 6 11 15 11 28 18 11 6
OS X 5 5 6 2 5 4 2 5 9 3 5 5 13 5 5 4 6 5 2 3 5 2 4 5 4 5 3 11 7 2 3 3 1 4 4 2 8 1 5 10 6 7 7 4 4 4
Linux 5 8 3 6 3 4 9 11 5 7 3 5 9 10 8 10 6 3 10 7 9 9 8 5 4 6 8 6 9 6 4 3 5 5 3 8 12 4 4 11 9 9 5 6 9 4

Readme

Source
raw.​githubusercontent.​com

PHP Getters and Setters

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

Features:

  • Generate Getters, Setters or Both
  • Can be applied to all class properties or just to a single one
  • Description, Type and Type Hinting automatically discovered from the variable docblock
  • fully customizable templates

Usage Instruction:

  1. Generate PHP code

    class test
    {
        /**
         * foo container
         *
         * @var AbcClass
         */
        private $foo;
    }
    
  2. Go to Tools -> PHP Getters and Setter

  3. 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!

Usage

Commands available are:

  • Generate Getters and Setters
  • Generate Getter
  • Generate Setter
  • Generate Getter for…
  • Generate Setter for…

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.

Settings Reference

ignore_visibility

type : boolean

default : false

description: ignore visibilty for setters generation

registerTemplates

type : array

default: []

description: the user templates to load

template

type : string

default: camelCaseFluent

description: the template to use

type_hint_ignore

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

setter_before_getter

type: boolean

default: false

description: Set to true to generate setter code before getters

Creating your own template

[package-dir] is your package directory.

  • Make a directory called [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; } ”“”

* Edit the parts between setter and getter how you want.
* Edit your user settings for this package. On OSX that'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",
* restart sublime to use the new template