开发者

PHP: Add constants to a class on runtime

i have the following code:

interface ITest {
    const SIGNAL_FOO = 'foo';
}
class SomeClass extends SignalSlotObject implements ITest {
    const SIGNAL_BAR = 'bar';
}

SignalSlotObject fetches all constants beginning with 'SIGNAL_' at runtime from the class and from all interfaces it implements. That's fine and works, but my problem is:

$c = new SomeClass();
$c->connect(SomeClass::SIGNAL_BAR, ...); //works well
$c->connect(ITest::SIGNAL_FOO, ...);     //also works well
$c->connect(SomeClass::SIGNAL_FOO, ...); //doesn't work

It would be great to let the 3rd option work as well because the programmer using SomeClass can't know, if this signal is defined in SomeClass or one of it's interfaces.

My intention was to dynamically add the constants defined in the interfaces to the class on runtime but I have no hint how to add a constant on runtime to a class. I also cannot use __s开发者_如何转开发et and __get because they do not work on a static scope (They would work for $c->SIGNAL_FOO but not for SomeClass::SIGNAL_FOO).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜