NYCPHP Meetup

NYPHP.org

[nycphp-talk] static variable variable?

Michael B Allen ioplex at gmail.com
Thu Dec 20 17:25:17 EST 2007


Actually this lead me to a solution that didn't use the static array
at all. I really just wanted the child class to be able to map some
strings. So I just some if return blocks in the overridden method:

    protected function mapName($name)
    {
        if ($name === 'foo') return 'bar';
        if ($name === 'zig') return 'zag';
        return $name;
    }

The parent version just returns $name unchanged. No need for the
static array at all.

Thanks,
Mike

On 12/20/07, Nick Galbreath <nickg at modp.com> wrote:
> 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
> >
>
>
> _______________________________________________
> 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
>


-- 
Michael B Allen
PHP Active Directory SPNEGO SSO
http://www.ioplex.com/



More information about the talk mailing list