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.
-
1I never said they can't have constants, my point was only methods seem to be required by the interfaces properties and constants don't seem to work as expected. Perhaps I should have been more clear when I said "only works for." – chrisw Mar 29 '11 at 14:48
4 Answers
http://php.net/manual/en/language.oop5.interfaces.php#language.oop5.interfaces.constants
Its possible for interfaces to have constants. Interface constants works exactly like class constants except they cannot be overridden by a class/interface that inherits it.
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.:
abstract class MyAbstract
{
abstract public function getPropertyA();
abstract public function getPropertyB();
}
Now, any class using extend MyAbstract
will be required to define getPropertyA()
and getPropertyB()
, which ensures the values will always be accessible.

- 9,640
- 4
- 43
- 72
-
1Gordon, they can if you have Object A implementing Interface A then Object B extending Object A and then have Object C extend Object B. Object C will be able to redefine the constant. Not sure if this is expected behavior or not? – chrisw Mar 29 '11 at 14:59
-
@chrisw let me try that. that sounds like a bug. can you put an example on codepad please? – Gordon Mar 29 '11 at 15:01
-
@chrisw There is a discussion going on about that in PHP. I believe so far the consensus is that it is "undocumented behavior" and should not be relied on (not sure whether it will be considered a "bug" or approved as a "feature"). – Unsigned Mar 29 '11 at 15:01
-
@Gordon Here's a comment on the PHP manual with demonstration code: http://www.php.net/manual/en/language.oop5.interfaces.php#102755 – Unsigned Mar 29 '11 at 15:02
-
@Unsigned Code labs: I doubt this would be considered a feature. A constant should be constant (what else?). They shouldn't be allowed to be redefined. – netcoder Mar 29 '11 at 15:05
-
2
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
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 .

- 9,577
- 2
- 39
- 43
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.

- 267,341
- 46
- 444
- 390