40

i wonder if there is any difference between

class TestClass {
    private $_var = "abc";
}

vs

class TestClass {
    private $_var;
    function __construct() {
        $this->_var = "abc";
    }
} 

i wonder if the latter is the preferred way/better practice? is there any functional difference?

Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
  • fwiw - actionscript warns against initializing objects in the class's declaration. So while `private $_var = "abc"` might be acceptable, `private $_var = new SpecializedClass();` would not be – Dan Heberden Jul 09 '10 at 03:49
  • Even many experience developers can confuse with this. Use the constructor for dynamic initialization. Here is a similar thread http://stackoverflow.com/questions/7088585/initializing-class-member-variables-with-expressions-concatenated-string-in-ph – kta Oct 22 '13 at 01:51

3 Answers3

28

They're effectively the same. I prefer the former, because then there's only one place to look for the value and its default.

On the other hand, if you need to do something dynamic with it or set it to anything other than an array or primitive, you need to use the second form. Notably, you can't use a function call to declare a variable in the first form.

Michael Louis Thaler
  • 1,854
  • 2
  • 15
  • 19
  • 1
    hmm, i guess that explains it, the 1st method can only be used for simple instantiation, the 2nd is if complex processing if required, eg. function calls or if/else etc – Jiew Meng Jul 09 '10 at 04:08
  • I think another good reason to prefer the former one, is that default values will remain even if you extend the class and overwrite the constructer. – Adam Nov 28 '17 at 21:02
4

Excellent question! I feel like the first example is more correct, if you already know the initial value of the object's attribute, why would you want to declare it in the constructor?

I feel like the purpose of the constructor is to set attributes that may be variable.

If anything, it seems like a readability thing. I don't know of any performance issues with either method.

bimbom22
  • 4,510
  • 2
  • 31
  • 46
  • you can have multiple constructors tho. one f.e. if user visits with chrome, or more generous, you can have different arguments for different constructors of the same object. overwriting constructors is not just a readability thing – clockw0rk Jun 12 '20 at 09:46
2

I am not aware of any differences in your examples, they both seem to behave the same. if you do both, constructor code overrides the initialization done in the declaration section.

Personally I come from C++ background and in statically typed languages all declarations happen inside the body of the class but outside of any functions, and all initializations and other class prep happen inside the constructor.

When initialization is done as in your first example and there is some code doing something in constructor as well, to me it looks like mixing coding paradigms, so even though it is more verbose, I tend to pick your second example for my own style of code.

Dennis
  • 7,907
  • 11
  • 65
  • 115