Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8908381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:09:51+00:00 2026-06-15T03:09:51+00:00

Before PHP 5.3 I was using the following home-made function to create enums :

  • 0

Before PHP 5.3 I was using the following home-made function to create enums :

function enum()
{
    for($enums = func_get_args(), $enum = reset($enums), $i = 1; $enum; $enum = next($enums), ++$i)
    {
        if(defined($enum)) throw new Exception($enum.' is already defined.');
        else define($enum, $i);
    }

} // enum()

And then :

enum('CONST0', 'CONST1', 'CONST2', ...);

With php 5.3 I can do that using const instead of define to benefit from namespaces. Is there a way I could modify this function to make it use the const keyword ?

Also I’m only using 5.3 to make my code look cleaner, if there must be a performance impact (i.e. using eval or stuffs like that), I’ll stick with the good old define.

I’ve already looked at this question : PHP and Enumerations but I didn’t find what I’m looking for.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:09:53+00:00Added an answer on June 15, 2026 at 3:09 am

    You can’t use const keyword conditionally [see this very high voted answer].

    If you want to namespace your constants, the best approach is to make them static fields of a class, taking advantage of magic __get and __set if you needed.

    EDIT: no magic setters / getters in static context of course, you can come with a flexible solution anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following statement which worked fine before PHP 5.3 using the split
I am using following code to populate DropDown: $(document).ready(function(){ $('#inDistrict').sSelect(); $(#inDistrict).change(function(){ $.getJSON(filldistricts.php,{id: $(this).val(), ajax:
I have a PHP script using Doctrine 2 which does essentially the following: $entityManager->transactional(function($em)
I have an HTML form POSTing to the following index.php: <?php require_once(/home/full/path/to/included/file.php); ?> And
I have the following php code in my view using the Global: PHP field
I'm using the following code: <?php if(extension_loaded(IonCube Loader)) { // Yes } else {
In a server sided script I am using the following PHP code to encode
I want to differentiate between the following 2 html tags using php + regex
I am using php CodeIgniter and have the following rewrite rule in my .htaccess
I'm using PHP's SwiftMailer library to bulk send emails (following CANSPAM and RFCs). Everything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.