Why declare vars in a cool constructor place?

Is there any good reason to declare variables used by the class outside the constructor function first?

class foo { 

    var bar;  // why is this a good practice? (or could it be skipped?)

    public function __construct() {
        $this->foo = 'foobar';
    }
}

I have seen this often, but I'm not sure what he is doing, as it seems that he works great to leave them.

+3
source share
2 answers

Declaring a property outside of the constructor actually declares it.

If you do not, it will be automatically created when the value affects it.


I see at least four main advantages of property declarations:


: , PHP , , .

+6

. , . IDE .

, , , , :)

+1

All Articles