Static member cannot be used as default function parameter in PHP5?

Dose php5 prohibits this use, the following code does not work

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

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

tt ("rcohu");

he 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
+3
source share
1 answer
function tt($para = Foo::$data)
{
    echo $para;
}

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

Just do the following:

function tt($para = false)
{
    if(!$para) $para = Foo::$data;
    echo $para;
}
+6
source

Source: https://habr.com/ru/post/1784455/


All Articles