How should I define constants within an object?

Hello,

What the title says…

I’m not sure if I should do it in the constructor or not, outside the class (but I’d rather not, those constants are really meant only for that class). Maybe it shouldn’t be constants, but simply variables?

It depends entirely on the context. Can you give an example (preferably usage example) of the kind of constant you’re referring to?

I.e. there are constants for default settings (e.g. Settings::DefaultModule or something) , constants referring to values (e.g. $Dave->setTieColour(Colour::Blue) or using binary flags, $Dave->setTieStyle(Ties::Blue | Ties::Stripy)), etc.

http://php.net/manual/en/language.oop5.constants.php

Since the constants are for the class only, why not just declare them as one of the class atrributes?

If it’s a constant for that class then you shouldnt use variables which are not constant but variable :smiley:

You can set class constants like this: (taken from http://php.net/manual/en/language.oop5.constants.php as mentioned earlier)

class MyClass
{
    const constant = 'constant value';

    function showConstant() {
        echo  self::constant . "\
";
    }
}