Reply to Re: Current class name for use with ::

Your name:

Reply:


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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация