NYCPHP Meetup

NYPHP.org

[nycphp-talk] static variable variable?

Nick Galbreath nickg at modp.com
Thu Dec 20 12:45:30 EST 2007


Hi,
if I understood your issue correctly, then in C++, Java, and PHP5 you'd do
something like this:

abstract class Foo {

public function doit()
{
echo $this->gettext();
}

abstract protected function gettext();
}

class Bar extends Foo {
static $msg = "this is some text\n";
public function gettext() {
return self::$msg;
}
}

$tmp = $Bar;
$tmp->doit();


Or you can get rid of the 'abstract' part and just define Foo::gettext() to
do "nothing" if that make sense.

Yeah the method to wrap the variable is a bit annoying, but you can think of
it this way... the parent class doesn't care if it's static or not.  It just
wants the variable.  The fact the children wanted to use a static is their
choice.

hope this made sense... looks like you found a another nifty way of doing
it.

enjoy!

--nickg




On 12/19/07, Michael B Allen <ioplex at gmail.com> wrote:
>
> I have a bunch of subclasses that define a static varible that I need
> to access in the parent class. So I'm trying something like:
>
> $_staticvarname = get_class($this) . '::$_staticvar';
> print_r($$_staticvarname);
>
> But this doesn't work. I just get:
>
>   Undefined variable: Sublcass::$_staticvar
>
> If I do:
>
>   print_r(Subclass::$_staticvar)
>
> it works so I know it's just an evaluation issue.
>
> I can't use const because the variable is an array. I don't want to
> hardcode the subclass name in the parent class and I want to use a
> static variable reasoning that it uses less memory.
>
> Is there any way to do this?
>
> Mike
>
> --
> Michael B Allen
> PHP Active Directory SPNEGO SSO
> http://www.ioplex.com/
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20071220/3d70b872/attachment.html>


More information about the talk mailing list