个性化阅读
专注于IT技术分析

如何在NetBeans中自动为PHP类创建getter和setter

当你使用PHP框架时, 尤其是使用Doctrine作为默认ORM的PHP框架之一, 你将能够使用几个命令从项目中已经存在的数据库中创建Entity类。这种方法的缺点之一是你将需要自己创建类的getter和setter方法, 因为该学说会生成类似以下内容的内容:

<?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;
}

好吧, 自己创建吸气剂和吸气剂不是一个坏主意, 但是如果实体具有20多个属性怎么办?当然, 如果你像我一样懒惰, 那不是正确的方法, 除了很可能你在”累”时会意外地从属性中切换。现代IDE出于某种原因而存在, 以使你的开发过程更加轻松。

NetBeans提供了插入代码工具, 该工具可以自动为你生成类的getter和setter, 我们将在本文中向你展示如何。

自动生成方法

你需要做的第一件事是打开” NetBeans代码插入”的上下文菜单, 可以通过将光标定位在应创建新方法的类中, 然后按组合键ALT + INSERT来执行此操作, 或者在Mac CTR + I中。按组合键后, 将显示上下文菜单, 如下所示:

NetBeans Getter和Setter for PHP类

你也可以通过”源代码”>”插入代码”中的NetBeans工具栏打开此上下文菜单。在此菜单中, 你可以选择是否仅创建吸气剂和吸气剂, 或同时创建两者。在这种情况下, 我们要

注意

如果类应在设置器中返回自身(链方法的典型行为), 请不要忘记选中流利的设置器复选框以在每个设置器上返回$ this。公共修饰符也是如此, 如果要使用公共方法, 请选中”使用公共修饰符”框。

NetBeans获取器和设置器

在这种情况下, 我们检查了Fluent Setter和Use Public Modifier以及我们所有的属性, 因此生成的代码将是:

<?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;
    }

}

编码愉快!

赞(0)
未经允许不得转载:srcmini » 如何在NetBeans中自动为PHP类创建getter和setter

评论 抢沙发

评论前必须登录!