Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP initialize variable in __construct() vs declaration

Tags:

php

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?

like image 713
Jiew Meng Avatar asked Jul 09 '10 03:07

Jiew Meng


People also ask

What is the difference between declaring and initializing a variable?

Declaration tells the compiler about the existence of an entity in the program and its location. When you declare a variable, you should also initialize it. Initialization is the process of assigning a value to the Variable. Every programming language has its own method of initializing the variable.

What is correct way to declare and initialize variable?

When you declare a variable, you should also initialize it. Two types of variable initialization exist: explicit and implicit. Variables are explicitly initialized if they are assigned a value in the declaration statement. Implicit initialization occurs when variables are assigned a value during processing.

What is __ construct in PHP?

PHP - The __construct FunctionA constructor allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class. Notice that the construct function starts with two underscores (__)!

Can we initialize variable in constructor?

Yes, you can also initialize these values using the constructor.


3 Answers

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.

like image 134
Michael Louis Thaler Avatar answered Oct 09 '22 00:10

Michael Louis Thaler


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.

like image 27
bimbom22 Avatar answered Oct 09 '22 00:10

bimbom22


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.

like image 40
Dennis Avatar answered Oct 09 '22 00:10

Dennis