NYCPHP Meetup

NYPHP.org

[nycphp-talk] Static Method Referencing

Hans Zaunere lists at zaunere.com
Tue Feb 15 11:12:35 EST 2005


> > 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






More information about the talk mailing list