title: PHP - Force a class to declare a property
body: <p>I have a set of classes which extend an abstract class. The abstract class contains methods that make use of late static bindings to get information which is defined in the child classes. So for example, the abstract class might have a method containing the line:</p>
  
  <pre><code>$var = static::$childVar . ' some text';
  </code></pre>
  
  <p>In this way, it is possible at runtime for the method defined in the abstract class to generate different strings based on the value of a variable defined in the child classes. </p>
  
  <p>What I want to do is to have some mechanism that forces the child classes to declare this variable, eliminating the possibility of the method being called if the variable is not set.</p>
  
  <p>Using interfaces, it is possible to require a class to define methods (and constants, which unfortunately cannot have their visibility modified using public/protected/private). So if a class implements an interface, but does not define all the methods specified in the interface, an error is generated. However, there is no way to use an interface to require a class to define a property.</p>
  
  <p>I have seen some material which suggests using getter methods in the interface (e.g. getChildVar()) to implicitly require variable declaration, but this is not suitable for me as I don't want the variables to be accessible outside the class (they have <code>protected</code> visibility).</p>
  
  <p>Similarly, constants will not work, because they do not support the <code>protected</code> visibility modifier.</p>
  
  <p>I know I could always use an <code>isset()</code> statement to check for this in the method (or even in the constructor) of the abstract class, but this seems inelegant compared to the use of interfaces for ensuring methods are defined. I am wondering if there is some standard way of doing this without hardcoding the requirements in my abstract class.</p>
  
  <p>Incidentally, if anyone knows why you can't declare variables inside interfaces, I'd be interested to know what the reasoning is. It seems like a major oversight to me. </p>
  
  <p>Any help will be much appreciated.</p>