如何在symfony 2 php的注释中访问类变量和常量 [英] how to access class variables and constants in annotation in symfony 2 php

查看:13
本文介绍了如何在symfony 2 php的注释中访问类变量和常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堂这样的课:

class Student {

const GENDER_MALE = "male", GENDER_FEMALE = "female";
/**
 * @var string $gender
 *
 * @ORMColumn(name="gender", type="string", length=50,nullable=false)
 * @AssertNotBlank(message="Gender cannot be blank",groups={"new"})
 * @AssertChoice(choices = {"male", "female"}, message = "Choose a valid gender.", groups={"new"})
 */
private $gender;

我必须对 "male""female" 值进行硬编码.有可能做这样的事情吗?

I have to hard code the values "male" and "female". Is it possible to do something like this ?

选择 = {self::GENDER_MALE, self::GENDER_FEMALE}

choices = {self::GENDER_MALE, self::GENDER_FEMALE}

推荐答案

这是 Doctrine2 注释阅读器(常量).

您的解决方案:

class Student
{
    const GENDER_MALE = "male", GENDER_FEMALE = "female";

    /**
     * @var string $gender
     *
     * @ORMColumn(name="gender", type="string", length=50,nullable=false)
     * @AssertNotBlank(message="Gender cannot be blank",groups={"new"})
     * @AssertChoice(
     *      choices = {
     *          Student::GENDER_FEMALE: "Female",
     *          Student::GENDER_MALE: "Male"
     *      },
     *      message = "Choose a valid gender.", groups={"new"}
     * )
     */
    private $gender;
}

这篇关于如何在symfony 2 php的注释中访问类变量和常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆