-1

How can I enforce constants in sub classes?

For example:

Class A implements B
{

}

Class B implements I
{

}


interface I
{

const bb = 'lr';

public function aa();

}

Above code ensures class A & B Must have aa(), but is there any way I can ensure class A & B must have a constant bb?

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
Sheldon Cooper
  • 236
  • 4
  • 17

1 Answers1

0

class A must "extend" (not "implement") B, since B is a class, not an interface.

class B must implement function aa (because you're implementing an interface that implicitly needs all of its functions to be implemented);

Other than that, both A and B have their bb const defined and can be accessed with A::bb and B::bb

Amarnasan
  • 14,939
  • 5
  • 33
  • 37