开发者

static member cannot be used as a default function parameter in PHP5?

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/s开发者_高级运维k/sk.php on line 6

Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING in /home/jw/sk/sk.php on line 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜