Note that there are some explanatory texts on larger screens.

plurals
  1. POPros and Cons of Interface constants
    text
    copied!<p>PHP interfaces allow the definition of constants in an interface, e.g.</p> <pre><code>interface FooBar { const FOO = 1; const BAR = 2; } echo FooBar::FOO; // 1 </code></pre> <p>Any implementing class will automatically have these constants available, e.g.</p> <pre><code>class MyFooBar implement FooBar { } echo MyFooBar::FOO; // 1 </code></pre> <p>My own take on this is <a href="https://stackoverflow.com/questions/5166087/php-global-in-functions/5166527#5166527">that anything Global is Evil</a>. But I wonder if the same applies to Interface Constants. Given that <em>Coding against an Interface</em> is considered good practise in general, is using Interface Constants the only constants that are acceptable to use outside a class context?</p> <p>While I am curious to hear your personal opinion and whether you use Interface constants or not, I'm mainly looking for objective reasons in your answers. I dont want this to be a Poll Type question. I'm interested in what effect using interface constants has on Maintainability. Coupling. Or Unit Testing. How does it relate to <a href="https://secure.wikimedia.org/wikipedia/en/wiki/Solid_%28object-oriented_design%29" rel="noreferrer">SOLID</a> PHP? Does it violate any coding principles that are considered Good Practise in PHP? You get the idea …</p> <p><strong>Note:</strong> <em>there is a <a href="https://stackoverflow.com/questions/320588/interfaces-with-static-fields-in-java-for-sharing-constants">similar question for Java</a> that listed some quite good reasons why they are Bad Practice, but since Java isn't PHP, I felt it justified to ask it within the PHP tag again.</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload