Learn how to create dinamically the getters and setters of a PHP class in netbeans.

When you work with a PHP Framework, specifically one of those that use Doctrine as default ORM, you will be able to create an Entity class from an already existent database in your project using a couple of commands. One of the disadvantages of this approach is that you will need to create the getters and setters of the class by yourself as doctrine will generate something like:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Games
 *
 * @ORM\Table(name="games")
 * @ORM\Entity
 */
class Games
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="bigint")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="slug", type="string", length=255, nullable=false)
     */
    private $slug;

    /**
     * @var string
     *
     * @ORM\Column(name="image", type="string", length=255, nullable=true)
     */
    private $image;

    /**
     * @var string
     *
     * @ORM\Column(name="short_description", type="string", length=500, nullable=true)
     */
    private $shortDescription;

    /**
     * @var string
     *
     * @ORM\Column(name="long_description", type="text", length=65535, nullable=true)
     */
    private $longDescription;

    /**
     * @var string
     *
     * @ORM\Column(name="embed_markup", type="text", length=65535, nullable=false)
     */
    private $embedMarkup;

    /**
     * @var integer
     *
     * @ORM\Column(name="times_played", type="bigint", nullable=false)
     */
    private $timesPlayed;

    /**
     * @var string
     *
     * @ORM\Column(name="tags", type="string", length=500, nullable=true)
     */
    private $tags;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_published", type="datetime", nullable=true)
     */
    private $datePublished;

    /**
     * @var boolean
     *
     * @ORM\Column(name="published", type="boolean", nullable=true)
     */
    private $published;
}

Well, creating the getters and setters by yourself isn't a bad idea, however what if the entity has more than 20 properties? Surely, if you are lazy like me, that's not the right way to go, besides it's very probable that you will switch from properties accidentally when you are "tired". Modern IDEs exist for some reason, to make your development process easier.

NetBeans offers the insert code tool that can generate the getters and setters automatically of a class for you and we'll show you how in this article.

Generating methods automatically

The first thing you need to do is to open the context menu of Insertion of Code of NetBeans, you can do this by locating the cursor in the class where the new methods should be created and then pressing the keyboard combination ALT + INSERT or if you are in Mac CTR + I. After pressing the combination, the context menu will appear and looks like this:

NetBeans Getters and Setters for PHP Class

You can as well open this context menu through the toolbar of NetBeans in Source > Insert Code . In this menu you can choose if you want to create only the getters and setters or both. In this case we want to

Note

If the class should return itself in the setters (tipical behaviour to chain methods), don't forget to check the fluent setter checkbox to return $this on every setter. The same goes for the public modifier, if you want public methods, check the Use Public Modifier box.

NetBeans getters and setters

In this case, we checked Fluent Setter and Use Public Modifier and all of our properties, so the generated code will be:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Games
 *
 * @ORM\Table(name="games")
 * @ORM\Entity
 */
class Games {

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="bigint")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="slug", type="string", length=255, nullable=false)
     */
    private $slug;

    /**
     * @var string
     *
     * @ORM\Column(name="image", type="string", length=255, nullable=true)
     */
    private $image;

    /**
     * @var string
     *
     * @ORM\Column(name="short_description", type="string", length=500, nullable=true)
     */
    private $shortDescription;

    /**
     * @var string
     *
     * @ORM\Column(name="long_description", type="text", length=65535, nullable=true)
     */
    private $longDescription;

    /**
     * @var string
     *
     * @ORM\Column(name="embed_markup", type="text", length=65535, nullable=false)
     */
    private $embedMarkup;

    /**
     * @var integer
     *
     * @ORM\Column(name="times_played", type="bigint", nullable=false)
     */
    private $timesPlayed;

    /**
     * @var string
     *
     * @ORM\Column(name="tags", type="string", length=500, nullable=true)
     */
    private $tags;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_published", type="datetime", nullable=true)
     */
    private $datePublished;

    /**
     * @var boolean
     *
     * @ORM\Column(name="published", type="boolean", nullable=true)
     */
    private $published;

    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getSlug() {
        return $this->slug;
    }

    public function getImage() {
        return $this->image;
    }

    public function getShortDescription() {
        return $this->shortDescription;
    }

    public function getLongDescription() {
        return $this->longDescription;
    }

    public function getEmbedMarkup() {
        return $this->embedMarkup;
    }

    public function getTimesPlayed() {
        return $this->timesPlayed;
    }

    public function getTags() {
        return $this->tags;
    }

    public function getDatePublished() {
        return $this->datePublished;
    }

    public function getPublished() {
        return $this->published;
    }

    public function setId($id) {
        $this->id = $id;
        return $this;
    }

    public function setName($name) {
        $this->name = $name;
        return $this;
    }

    public function setSlug($slug) {
        $this->slug = $slug;
        return $this;
    }

    public function setImage($image) {
        $this->image = $image;
        return $this;
    }

    public function setShortDescription($shortDescription) {
        $this->shortDescription = $shortDescription;
        return $this;
    }

    public function setLongDescription($longDescription) {
        $this->longDescription = $longDescription;
        return $this;
    }

    public function setEmbedMarkup($embedMarkup) {
        $this->embedMarkup = $embedMarkup;
        return $this;
    }

    public function setTimesPlayed($timesPlayed) {
        $this->timesPlayed = $timesPlayed;
        return $this;
    }

    public function setTags($tags) {
        $this->tags = $tags;
        return $this;
    }

    public function setDatePublished(\DateTime $datePublished) {
        $this->datePublished = $datePublished;
        return $this;
    }

    public function setPublished($published) {
        $this->published = $published;
        return $this;
    }

}

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors