NYCPHP Meetup

NYPHP.org

[nycphp-talk] Static Method Referencing

Jeff Knight jeff.knight at gmail.com
Wed Feb 16 10:42:14 EST 2005


You are the one that must be high to not see it... trying to eat your
cake and have it too. You're trying to be slick, sneak around the gate
and get away with something. The ultimate fact of the matter is that
you cannot access the contents of the $StaticMethod  property
("SayHello"), without instantiating the object. It does not matter
that you are then trying to do a perfectly legal operation with that
information, but that you do not have access to that information in
the first place.


On Wed, 16 Feb 2005 09:32:36 -0500 (EST), Adam Maccabee Trachtenberg
<adam at trachtenberg.com> wrote:
> 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!
> _______________________________________________
> New York PHP Talk Mailing List
> AMP Technology
> Supporting Apache, MySQL and PHP
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.nyphp.org
>



More information about the talk mailing list