Jump to content

static Methods: different behavior


arnoschaefer

Recommended Posts

Hi,

 

I have this bit of code:

 

class bla {

    static public function foo () {

    }

}

 

$classname = "bla";

$classname::foo ();

 

in my installation of PHP 5.2.4RC1 (OpenSuSE 10.2), this works fine. On another server with PHP 5.2.2, this results in

 

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /www/htdocs/w0088307/blog/version/test/tt2.php on line 9

 

(PAAMAYIM_NEKUDOTAYIM appears to mean double colon (::)).

 

Can anyone tell me if this is a change between version 5.2.2 and 5.2.4 (I did not find anything in the ChangeLog), or if this is a configuration problem? Is the above code legal at all?

 

Best Regards,

 

Arno

 

 

Link to comment
Share on other sites

Looks legal to me, I use this type of stuff quite a bit without issue. I can't say Ive tried with static methods though.

 

Changes between 5.2.2 and 5.2.4 should merely be bug fixes, not feature changes. I'm sorry, but I can't think of any config option that may cause this either.

Link to comment
Share on other sites

> What about bla::foo(); I think that is what you are looking for.

 

Well, the whole point of using a variable $classname is that I don't know which class I will be using in advance. Otherwise I would obviously use the classname directly as you suggest. But thanks anyway.

 

Link to comment
Share on other sites

I'm at home now using 5.2.2 I get the same error as you. It must be a caveat with static methods. This....

 

<?php

class bla {
    public function foo () {
        echo "foo!";
    }
}

$class = 'bla';
$obj = new $class;
$obj->foo ();

?>

 

works as expected, but obviously its not what you want. Sorry, but I can't seem to help.

Link to comment
Share on other sites

> $class = 'bla';

> $obj = new $class;

> $obj->foo ();

 

Yes, that was what I used as a workaround so far, but I just found a hint in the PHP doc on the "scope resolution operator" (double colon) and they had another workaround that is somewhat more elegant and does not require instantiating an object:

 

eval ("$classname::foo ();");

 

or, with an additional parameter and return value:

 

eval ("\$result = $classname::foo (\$parameter);");

 

(note the escaped $ signs).

 

There was a discussion that $class::foo() does not work, but why it suddenly started working in version 5.2.4 is still an interesting question. Maybe it was regarded as a bug fix. Unfortunately, there does not seem to be a change log yet for the 5.2.4 release candidates.

 

Thanks anyway for your help and effort.

 

Link to comment
Share on other sites

Though I can't seem to find it on their website, I found something that claimed this to be part of the changelog: "Added possibility to call static class members using variables."

 

It would seem you can't do it in previous version.

 

Yes, that's most likely it. Thanks a lot for the info.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.