PHP class property Using range ()

Here is the minimal test case I highlighted:

<?php

class What {
    public $foo = range(0,5);
}

?>

I do not know why this causes an error:

PHP password analysis error: syntax error, unexpected '(', waiting ',' or ';' in TestCase.php on line 4

Use array()works.

Using PHP 5.3.3 (bundled with OS X).

+3
source share
4 answers

You can only assign constant values ​​in this context. You will need to initialize $fooin the constructor if you want to use the return value of the function.

<?php

class What {
    public $foo;

    public function __construct() {
        $this->foo = range(0,5);
    }
}

?>

BTW: As others have noted, is array()not a function. This is a language construct.

+5
source

, . .

+1

, ​​ : -

http://www.phpbuilder.com/board/showthread.php?t=10366062

range() PHP.Net Manual, , $foo public, PHP PHP.

, .

0
source

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


All Articles