开发者

create class directly after a property field? [duplicate]

开发者 https://www.devze.com 2022-12-28 01:46 出处:网络
This question already has answers here: Closed 10 years ago. Possible Duplicate: declare property as object?
This question already has answers here: Closed 10 years ago.

Possible Duplicate:

declare property as object?

in java you can create an object directly after the property field like this:

but it seems not working for php:

class Test {
    public $object = new Object();
}
开发者_Python百科

you have to create it in the __construct() and assign it to the property?

thanks


From php.net

This declaration 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.

So no, you cant initialize it to an object. You'll have to do it in the constructor like you said

0

精彩评论

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