Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

PHP constant problem

  • 13-03-2013 4:46pm
    #1
    Registered Users Posts: 160 ✭✭


    Hi there

    I'm working on a class in a PHP web app which defines a number of class constants. It turns out that we want these constants to be initialised to different values and not modified again, depending on a certain condition being true.

    I realise class constants can only be assigned values when they are declared, and that a variable is more appropriate for what we want, but these constants are referred to in hundreds of places, and I want to minimise the code change elsewhere in the app.

    I've tried creating two new classes, each containing a set of consts with names common to both files, but assigning them different values. Depending on a particular conditional statement being true or false, I then include one of the class files and assigning the original class constants I mentioned the values in the included PHP file.

    However, this seems to work in some places and not in others. In some files that include the class with the constants defined, they successfully reference the constants and they contain the correct value.

    In other files though, though, I simply get an error :" Undefined class constant 'Classname::constantname.' The files that experience the error are in a different directory, but the include paths are accurate, it's just it doesn't seem to know anything about the values.

    Any ideas? Cheers


Comments

  • Registered Users Posts: 6,464 ✭✭✭MOH


    From a long-term maintainability point of view, you might be better biting the bullet and changing them to variables. Having "constants" that effectively aren't is likely to end up confusing anyone who ends up maintaining the code later.

    As it stands, are you 100% sure the class file is being included? Can you create an instance of the class OK?


Advertisement