What is the diff between constant and final keyword in php?

final

The methods or classes can not be modified by a child class. This prevents class inheritance, method-overriding and/or redefinition of methods.

Only class definitions and/or methods inside a class can be defined as final.

static

Declares class methods or properties as a static value so that you have access to them without instantiating an object. These are shared between parent and child-classes.

A class definition can not be static unlike final.

const

These create a constant value for a class. The constant values will get changed and can NOT be changed by a method in either parent or child-class.

Class constants are allocated per instance of the class.


const is a type specifier in itself. It can not be put along with public/private/static etc. final, as mentioned before can be used along with any method or class definitions and hence; applicable with all of them. static can not be applied to class definitions but can be used for class properties.

UPDATE

modifiers are allowed for class constants since PHP 7.1.0.

class Foo {
    public const bar = 5;
    private const baz = 6;
}

To summarise, final static can not be used to define something like:

class X {
    final static x = 5;
}

which is why you have a const.

What is the difference between constant and final keyword?

The only difference between final and const is that the const makes the variable constant from compile-time only. Using const on an object, makes the object's entire deep state strictly fixed at compile-time and that the object with this state will be considered frozen and completely immutable.

What is final keyword in PHP?

The final keyword is used to prevent a class from being inherited and to prevent inherited method from being overridden.

What is difference between constant and variable in PHP?

PHP Constants: PHP Constants are the identifiers that remain the same. Usually, it does not change during the execution of the script. They are case-sensitive. ... PHP..

What is the difference between static and constant in PHP?

Constant is just a constant, i.e. you can't change its value after declaring. Static variable is accessible without making an instance of a class and therefore shared between all the instances of a class.