3 Replies - 514 Views - Last Post: 01 April 2013 - 05:57 AM Rate Topic: -----

#1 P.Conrad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-March 13

enum name

Posted 01 April 2013 - 05:34 AM

Hi,

lets say we got those enum

class Days {

const SUN = 1;
const MON = 2; etc.

}
.

Is there any smooth way to print also enum name rather than only value, like i.e. name() method in Java for enums ?
Is This A Good Question/Topic? 0
  • +

Replies To: enum name

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3741
  • View blog
  • Posts: 13,092
  • Joined: 12-December 12

Re: enum name

Posted 01 April 2013 - 05:48 AM

PHP does not have enums (enumerations) but your code is the nearest equivalent.

It is possible to obtain the name using reflection, discussed at SO:

http://stackoverflow...of-the-constant

However, if you are doing this for this specific class, I would just create a method using a switch statement, or an array lookup, to obtain the name (the text) of the day.

This post has been edited by andrewsw: 01 April 2013 - 05:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: enum name

Posted 01 April 2013 - 05:56 AM

View Postandrewsw, on 01 April 2013 - 01:48 PM, said:

PHP does not have enums (enumerations) but your code is the nearest equivalent.

except for the SplEnum class (although it might be a bit different than in other PLs).

This post has been edited by Dormilich: 01 April 2013 - 05:57 AM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,796
  • Joined: 08-August 08

Re: enum name

Posted 01 April 2013 - 05:57 AM

Of course, if you're trying to get a day of the week number you can use date("N") to get numbers 1 - 7 for Monday through Sunday or date("w") to get 0 - 6 for Sunday through Saturday.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1