I've had this kind of philosophical debate with myself before. Here's where I stand most of the time although I realize this is an opinion-based answer:
One thing I see that might help answer the question is the passing of $params which may or may not have attributes/array members that are set.
Over the years I've come to this conclusion:
Avoid the passing of arrays.
Why? Well, there is no way to set or have sentinel values defined for optional passed arguments.
In other words, with the code you specified, you can't do something like this:
function __construct($arg1 = NULL, $arg2 = DEFAULT_VAL) {
$this->arg1 = $arg1;
$this->arg2 = $arg2;
}
$arg1 and $arg2 are optional arguments - if not passed they have NULL and DEFAULT_VAL respectively - no need to check explicitly.
Maybe this seems kind of arbitrary.
I think I get what you're trying to accomplish - the passing of a single reference as opposed to tons of arguments. This brings me to my next consclusion:
If not passing "atomic" variables (strings, integers, literals) then pass objects.
There are performance benefits here since passing objects is done by reference (although arrays I think are the roughly the same inside PHP).
So you might do something like:
function __construct(MyAwesomeObject $oArg) {
$this->oArg = $oArg;
}
The passed object argument would then be guaranteed to have "property1", "property2" albeit possibly with default values themselves.
Additionally, here you can type hint and a good IDE will correctly autosuggest code completion as well.
But we quickly realize we have a chicken and an egg thing going on: you're constructing an object with passed object arguments that themselves need constructing at some point.
So where does this leave us? Well, I've come to the conclusion that eventually all classes distill down to, for lack of a better term, "atomic" variables (strings, floats, doubles, ints, resources you get my point), and that I tend to try to construct all classes with those variable types or objects - but not arrays.
So did I answer your question? probably not exactly. But I hope I illustrated something useful albeit somewhat stylistic. I think the code is a bit cleaner, more readable, and less costly.
Now, this isn't to say you shouldn't sanitize your input. That's another discussion entirely.
Hope this helps.
The initialization process may look the same, but a constant cannot be changed once it is set. Depending on the type of variable (private, protected, public), other things, such as the current class, subclasses, or other classes, can act on and may have the ability to change the value of that variable. A constant stays the same for all things using it.
So lets say you initialize a variable to say:
var normalVariable = 10;
Later in your code you could change it to 20, or something, if you wanted. With a constant (which is usually named in all caps LIKE_THIS), you couldn't change the value to something else, you would most likely get an error if you tried.
For the most part, a variable is what it claims to be. It is something that may not always be a certain value. A constant on the other hand would be something that you want to signify as something that will not change. Think gravity or Pi in the real world.
Best Answer
In languages like C, newly-declared variables essentially point to a (more or less) random memory location. If you declare the variable without also initializing it, it will contain whatever random value the memory happens to contain at the location in memory that the new variable points to.* Using the variable in this state will cause unpredictable behavior.
Assigning a value to the variable means that the memory location is set to the value that you have assigned, so that you and your program now have an expectation of what that variable contains.
*This description is deliberately simplified. Your actual mileage may vary.