NYCPHP Meetup

NYPHP.org

[nycphp-talk] Static Method Referencing

Hans Zaunere lists at zaunere.com
Mon Feb 14 12:50:36 EST 2005


Given this test case:

<?php
class MyClass
{
   private $StaticMethod = 'SayHello';

   static public function SayHello( $name ) {

      echo "\n\nSay Hello, $name!\n\n";
   }

   public function HelloWorks() {

      $tmp = $this->StaticMethod;
      MyClass::$tmp('Works');
   }

   public function HelloNoWorks() {

      MyClass::$this->StaticMethod('NoWorks');
   }
}


$myobj = new MyClass;
$myobj->HelloWorks();
$myobj->HelloNoWorks();


?>

Results in this:


Say Hello, Works!

PHP Fatal error:  Call to undefined method MyClass::StaticMethod() in /home/praxis/test.psh on line 22


Line 22 is:

      MyClass::$this->StaticMethod('NoWorks');


If I change line 22 to:

      MyClass::{$this->StaticMethod}('NoWorks');


Then I get a parse error:

PHP Parse error:  parse error, unexpected '{', expecting T_STRING or T_VARIABLE or '$' in /home/praxis/test.psh on line 22

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?


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