NYCPHP Meetup

NYPHP.org

[nycphp-talk] Static Method Referencing

Adam Maccabee Trachtenberg adam at trachtenberg.com
Wed Feb 16 09:32:36 EST 2005


I believe you can also use the reflection classes, if you're into an
all OO solution.

-adam

On Tue, 15 Feb 2005, Daniel Kushner wrote:

> Hi Hans,
>
> I can't seem to find a way to do this by dereferencing, but yet
> another way of doing it:
>
> call_user_func(array('MyClass', $this->StaticMethod), 'NoWorks');
>
> -Daniel
>
>
>
> On Tue, 15 Feb 2005 11:12:35 -0500, Hans Zaunere <lists at zaunere.com> wrote:
> >
> > > > I this expected behavior or a bug?  Do I have to set the method's
> > > > name to a $tmp variable before using it as a reference to a static
> > > > method?  Is there some other way to do it that I'm forgetting?
> > >
> > > self::staticMethod('foo');
> > >
> > > Pages 28-9, 35 in "Upgrading to PHP 5." :)
> >
> > I don't have the book, but from the code snippet above, that's not it anyway.  StaticMethod is a property, containing the name of a static method to call.
> >
> > Maybe my choice of naming the property was confusing, so here is another:
> >
> > class MyClass
> > {
> >    private $StaticMethodName = 'SayHello';
> >
> >    static public function SayHello( $name ) {
> >
> >       echo "\n\nSay Hello, $name!\n\n";
> >    }
> >
> >    public function HelloWorks() {
> >
> >       // this dereferences the static method call, calling the SayHello() method
> >       $tmp = $this->StaticMethodName;
> >       MyClass::$tmp('Works');
> >    }
> >
> >    public function HelloNoWorks() {
> >
> >       // this does not
> >       MyClass::$this->StaticMethodName('NoWorks');
> >
> >       // also does not work (parse error)
> >       // MyClass::{$this->StaticMethodName}('NoWorks');
> >    }
> > }
> >
> > $myobj = new MyClass;
> > $myobj->HelloWorks();
> > $myobj->HelloNoWorks();
> >
> > Perhaps this is just a limitation of the parser, but I don't see any reason it shouldn't work, so perhaps a bug.
> >
> > Basically I'm seeing that's there's no way to dereference a property into a static method call, whereas a regular variable can be dereferenced in this respect.
> >
> > For additional clarification of the underlying goal, a non-OOP example:
> >
> > function my_func() {
> >    echo 'This is my_func';
> > }
> >
> > $thefunc = 'my_func';
> > $thefunc();
> >
> > Calls a function by the name of my_func()
> >
> >
> > ---
> > Hans Zaunere
> > President, Founder
> >
> > New York PHP
> > http://www.nyphp.org
> >
> > AMP Technology
> > Supporting Apache, MySQL and PHP
> >
> > _______________________________________________
> > New York PHP Talk Mailing List
> > AMP Technology
> > Supporting Apache, MySQL and PHP
> > http://lists.nyphp.org/mailman/listinfo/talk
> > http://www.nyphp.org
> >
> _______________________________________________
> New York PHP Talk Mailing List
> AMP Technology
> Supporting Apache, MySQL and PHP
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.nyphp.org
>

-- 
adam at trachtenberg.com | http://www.trachtenberg.com
author of o'reilly's "upgrading to php 5" and "php cookbook"
avoid the holiday rush, buy your copies today!



More information about the talk mailing list