3

dose php5 forbid this usage, the following code doesn't work

class Foo{
    public static $data = "abcd";
}

function tt($para = Foo::$data)
{
    echo $para;
}

tt ("rcohu");

it reports:

PHP Parse error:  syntax error, unexpected T_VARIABLE, expecting T_STRING in /home/jw/sk/sk.php on line 6

Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING in /home/jw/sk/sk.php on line 6
1
  • if you are happy with my answer, can you mark it as such Commented Jan 12, 2011 at 3:34

1 Answer 1

6
function tt($para = Foo::$data)
{
    echo $para;
}

function definitions can only contain simple assignments, not complex ones like Foo::$data.

Just do this:

function tt($para = false)
{
    if(!$para) $para = Foo::$data;
    echo $para;
}
Sign up to request clarification or add additional context in comments.

2 Comments

They just cannot contain variable expressions. Foo::DATA where DATA is a class constant works.
True, so if you had define('DATA', 'my-data'); you could use function tt($para=DATA){

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.