|
Posted by Chung Leong on 05/03/06 18:50
Andy Jeffries wrote:
> OK, assuming the following class structure:
>
> class A {
> public function printConst()
> {
> print {something}::TEST_CONST;
> }
> }
>
> class B extends A {
> const TEST_CONST = 10;
> }
>
> class C extends A {
> const TEST_CONST = 20;
> }
>
> So, I want to be able to do as follows:
>
> $b = new B();
> $b->printConst();
> $c = new C();
> $c->printConst();
>
> What I want to know is what should go instead of {something}. I don't
> want the class it's declared in, but the class it's being called from.
>
> I'm sure there's a way to do it, I remember reading it when studying for
> the ZCE, but I'm damned if I can find it now when I need it.
You can, of course, obtain the class name of the instance by calling
get_class() on $this. The $class::member construct is illegal though,
you'll end up with something like:
class A {
public function printConst()
{
$class = get_class($this);
eval("print self::TEST_CONST;");
}
}
[Back to original message]
|