3

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();
}

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

thanks

Community
  • 1
  • 1
never_had_a_name
  • 90,630
  • 105
  • 267
  • 383

1 Answers1

2

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

Galen
  • 29,976
  • 9
  • 71
  • 89