Is it possible to require objects in PHP 5.3 to contain certain constants or properties? I read a bit on PHP's interface model and it seems it only works for abstract public methods so that's right out. So what i'm asking is if there is a way to have two objects Object A and Object B. If Object B wants to extend Object A it must contain a particular constant or variable. How would you design this type of architecture? Thanks.
Can you force child classes to override constants?
135 Views Asked by chrisw AtThere are 4 best solutions below

Object A and Object B should be classes, objects are instances ( of classes ) that can't be extended ( i might be mistaken but ... ) .
With that in mind, you can let's say Class B extends Class A, then if you whant Class B to have a particular constant defined , Class A should implement an interface . Read more about php interfaces .

Hmm, if Object B extends Object A, and Object A contains a constant or variable, then Object B will also contain them if the variable is protected
or public
. So if you define the variable in Object A, even if its value is NULL
, Object B will by definition also define it.
You cannot force Object B to implement a constant that is not inherited from Object A. Same with an interface -- if B implements interface C, which contains constants, it inherits them. However, B can define different constant values than its parent class or interface.

Yes and no.
No, because properties are an implementation detail. It is not part of the interface at all (methods define the interface). You can define properties (in classes) and constants (in classes or interfaces), but you cannot mark them abstract and require them to be "implemented" by derived classes.
Yes, because interface allows you to define constants that all derived classes will have as well (be careful with that, see @Gordon's question). This "enforces" classes to have these constants. You can also "enforce" properties to be passed to derived classes using abstract classes or regular inheritance:
abstract class Foo {
public $foo = 'bar';
protected $bar = 'baz';
}
class Bar extends Foo {
public funciton getBar() {
return $this->bar;
}
}
$bar = new Bar;
echo $bar->foo; // bar
echo $bar->getBar(); // baz
http://php.net/manual/en/language.oop5.interfaces.php#language.oop5.interfaces.constants
Interface constants cannot be overridden by inheriting classes. Class constants can be overridden, but are not required to be (even if declared in an abstract class). Abstract classes are designed to enforce interface, not implementation. Constants fall under implementation, while methods define the interface. Thus, while constants can be declared with a default value in an abstract class, it is up to the child to decide whether to use or redefine them, or not.
Your best alternative is to use "getter" methods, e.g.:
Now, any class using
extend MyAbstract
will be required to definegetPropertyA()
andgetPropertyB()
, which ensures the values will always be accessible.