-1

What would be the equivalent of public static final String str = "Foo"; in PHP?

I have a class like so:

class MyClass {

    public $TYPE_REGULAR = 'regular';

    public function display($type) {
        if($type===$this->TYPE_REGULAR) {
            return "This is a regular object";
        }
        return "This is not a regular object";
    }
}

In the above example, I don't want $TYPE_REGULAR to be a member property. I want it to be something like public static final String of Java.

Thanks.

littleibex
  • 1,705
  • 2
  • 14
  • 35

2 Answers2

4

In PHP you have to use const for variables.

You can use final only for classes and methods.

In your situation:

const TYPE_REGULAR = 'regular'; // public is by default

Also read more about difference between static and const in PHP: PHP5: const vs static

Community
  • 1
  • 1
Kristiyan
  • 1,655
  • 14
  • 17
0

Use const.

...

const TYPE_REGULAR = 'regular';

...

// access it inside the class using
self::TYPE_REGULAR

See Documentation.

Jake Opena
  • 1,475
  • 1
  • 11
  • 18