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 745863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:37:57+00:00 2026-05-14T09:37:57+00:00

I am using one separate file for all constants of my PHP application. class

  • 0

I am using one separate file for all constants of my PHP application.

class constants
{
    const USERNAME = 'abc';
    /*
      ⋮
    */
}

For lets say USERNAME constant, value can be either xyz or abc based on file exists check. If xyz file exists USERNAME value would be xyz. How can I do this check in my constants class?

  • 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-05-14T09:37:58+00:00Added an answer on May 14, 2026 at 9:37 am

    If the value of the constant is variable, then it is not a constant, but a variable.

    Since you are (correctly) trying to keep your application stuff capsuled and away from the global scope, you might be interested in the Registry Pattern. A Registry is basically a glorified array that stores whatever you throw into it and is globally accessible throughout your application.

    Some resources:

    • http://www.patternsforphp.org/index.php?title=Registry
    • http://www.brandonsavage.net/the-registry-pattern-reexamined/
    • http://blog.fedecarg.com/2007/10/26/registry-pattern-or-dependency-injection-container/

    EDIT If you are truly desperate and have to have the constant redefined, you can use

    • runkit_constant_redefine — Redefine an already defined constant

    Runkit might not be available if you are on shared hosting and I consider needing it a code smell, but here is how you’d basically do it (in your bootstrap)

    if ( file_exists('xyc') ) {
        runkit_constant_redefine('Constants::USERNAME', 'xyz');
    }
    

    EDIT Some more options (all of which not exactly pretty either):

    class Abc { const FOO = 1; const BAR = 2; }
    class Xyz extends Abc { const FOO = 2; }
    class_alias(file_exists('abc') ? 'Abc' : 'Xyz', 'Constants');
    

    For this you would rename your current constants class to Abc and add a second class Xyz to extend it and overwrite the USERNAME constant (FOO in the example). This would obviously break your code, because you used to do Constants::USERNAME, so you have to create an alias for the former class name. Which class Constants will point to, is decided with the conditional check. This requires PHP5.3.

    A pre-5.3 solution would be to simply save the Constants class file under two different names, e.g. abc_constants.php and xyz_constants.php, modify the latter accordingly to hold USERNAME xyz and then include either or depending on the file check.

    Or replace the value of USERNAME with a placeholder and instead of including the class you load it into a variable as a string. Then you replace the placeholder according to the filecheck result and eval the string, effectively including the class this way.

    But I have to say it again: I strongly suggest refactoring your code over using these.

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

Sidebar

Related Questions

Some time back, I created two separate SharePoint projects in VS 2008; one using
Iam developing one application.In that iam using one uiimageview.And take another image view and
I am using one .g file for both lexer and parser. I have the
Say, for example, I have many web applications using the same class library compiled
Using the Qt Visual studio integration, adding a new Qt class adds two separate
When uploading one file using <input type=file /> in Opera, it works as expected.
When using one of the various JDBC template methods I am confused on how
I am using one signal and slot connection in a block. My code as
I m using one EditText field and one spinner. I have to pass teh
I am using one master page.In that I am registering java scripts and checking

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.