Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing variables outside of PHP constructor

I was looking through the PHP documentation and saw several comments where a variable was initialized outside of a class's constructor, similar to the following:

classMyClass {
    private $count = 0;

    public function __construct() {
        //Do stuff
    }
}

In PHP Objects, Patterns, and Practice, the author recommends using constructs only for the initialization of properties, deferring any heavy lifting or complex logic to specialized methods. This tutorial (a quick example that I found on Google) also recommends using constructors to initialize properties: http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-3.php.

Why would you want to initialize a variable outside the constructor? Is this just sloppy coding, or is there a reason to do something like this? I have to say that until recently, I initialized default values outside the constructor, and there doesn't seem to be any programmatic advantage of one way over the other.

like image 855
Andrew Avatar asked Jun 28 '12 16:06

Andrew


2 Answers

When you initialize a variable outside of the constructor, it must be initialized as a constant. You can't do any operation to initialize it. Thus, the initial value of that member is actually a part of the class signature.

For example, this is invalid:

private $var = $othervar;
private $var = func();

You could do it in the constructor as well, but it would be a bit more verbose and add some clutter to the constructor unless there was some sort of logic going on.

like image 145
Explosion Pills Avatar answered Sep 20 '22 10:09

Explosion Pills


More a comment than an answer, but please elaborate here a little:

Since it is recommended to use constructors for property initialization only,

Who says this and why? I assume the only relates to something else than property definitions with default values.


An answer part:

By default in PHP variables do not need to be defined because variables are then defined when first accessed in a write context. All variables, including undefined ones contain NULL (Demo):

class A {}

$a = new A;

var_dump($a->property); # NULL

Introducing class variables (properties) PHP then allowed to actually define variables. Those still return NULL by default, but they are defined (Demo):

class A {
    public $property;
}

$a = new A;

var_dump($a->property); # NULL

In the next step of the evolution, this language construct also allows to specify a constant expression. That is constant because definition is compile-time (not run-time as the when the constructor is invoked). An example (Demo):

class A {
    public $property = 'hello';
}

$a = new A;

var_dump($a->property); # string(5) "hello"

As this is compile- but your constructor run-time, I find it hard to compare both feature with another. Also it's not clear why you say that initializing via the constructor is recommended.

like image 40
hakre Avatar answered Sep 23 '22 10:09

hakre