Initializing variables outside of PHP constructor

你离开我真会死。 提交于 2021-02-18 22:08:17

问题


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.


回答1:


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.




回答2:


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.




回答3:


Far from sloppy... it's good programming practice. As you would also do in Java/C++, it just sets them up, and then you can do any initialisation in the constructor - usually to sent them to non-defaults as such.



来源:https://stackoverflow.com/questions/11248569/initializing-variables-outside-of-php-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!