开发者

PHP Class Property Using range()

开发者 https://www.devze.com 2023-01-29 00:22 出处:网络
Here is a minimal test case I\'ve isolated: <开发者_StackOverflow中文版;?php class What { public $foo = range(0,5);

Here is a minimal test case I've isolated:

<开发者_StackOverflow中文版;?php

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

?>

I have no idea why this produces an error:

PHP Parse error: syntax error, unexpected '(', expecting ',' or ';' in TestCase.php on line 4

Using array() works.

Using PHP 5.3.3 (bundled with OS X).


You can only assign constant values in that context. You'll have to initialize your $foo in a constructor if you want to use the return value of a function.

<?php

class What {
    public $foo;

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

?>

BTW: As others have pointed out, array() is not a function. It's a language construct.


Array isn't a function, it's a language construct. That's why it's allowed.


Class member variables are called "properties" ... may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

http://www.php.net/manual/en/language.oop5.properties.php


Check this link as a similar problem was sent and had been solved :-

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

Also you can see the examples of using range() function in PHP.Net Manual although I think the problem may be in the variable $foo and public keyword as you there may be a type mismatch or a conflict beteen the version of the PHP function and your running PHP version.

I hope this answer helps you..

0

精彩评论

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