Skip to content
Advertisement

Abstract constants in PHP – Force a child class to define a constant

I noticed that you can’t have abstract constants in PHP.

Is there a way I can force a child class to define a constant (which I need to use in one of the abstract class internal methods) ?

Advertisement

Answer

A constant is a constant; there is no abstract or private constants in PHP as far as I know, but you can have a work around:

Sample Abstract Class

JavaScript

This would run fine

JavaScript

Bar would return Error

JavaScript

Songo would return Error

JavaScript

Enforcer Class

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement