开发者

Initializing properties in PHP

开发者 https://www.devze.com 2023-03-27 10:41 出处:网络
While I\'m led to believe its likely just a design choice, is there any ad开发者_如何学JAVAvantage to initializing properties in PHP to an explicit null?

While I'm led to believe its likely just a design choice, is there any ad开发者_如何学JAVAvantage to initializing properties in PHP to an explicit null?

As a force of habit I find myself doing:

// ...
protected $_foo = null;
protected $_bar = null;
protected $_baz = null;
// ...

Of course, under circumstances where actual data is intended to be present at object instantiation, there is purpose:

// ...
protected $_array = array('a', 'b', 'c');
protected $_boolean = true;
// ...

Is omission of a null initialization value completely functionally equivalent to inclusion of a null initialization? Are there any other caveats? Also, unless a property is type-checked before any assignments would be made, initialization to an empty array seems like similar situation (and I find myself doing that constantly)


Yes,

protected $_foo = null;
protected $_foo;

are completely equivalents.
As for me great choise is

  • initialize clearly by null, if it will null by default
  • don't initialize, if it will be overriden in constructor

It helps you to see default values quickly, helps code be self-documenting

Don't initialize array by array() seems to be bad idea because you can't use some function (ex. array_push, array_map)


PHP 7.4 introduced a new feature called "typed properties" which adds the existing PHP type system to class properties. This means that you can now enforce which types a property has without having to encapsulate it in an object.

Typed properties are a very useful feature, however, typed properties without a default value have a new state: uninitialized. And if they're accessed before being initialised, you'll run into a Typed property must not be accessed before initialization error.

So the correct answer to OPs question if you're using PHP 7.4+ is yes, it does matter whether or not you explicitly initialise your class variables and the following two lines are not equivalent any more:

protected int $_foo = null;
protected int $_foo; // The default value in PHP 7.4+ is now uninitialized, not NULL

Further reading (no relation): https://madewithlove.com/typed-property-must-not-be-accessed-before-initialization/


is omission of a null initialization value completely functionally equivalent to inclusion of a null initialization?

yes

initialization to an empty array seems like similar situation

no. You can try foreach ($this->arr) (or something else) and that variable should be initialized with array to avoid notice.


Properties are implicitly initialized to NULL, there is no advantage to do this explicitly.


No, ...

...it is not necessary to initialize variables in PHP however it is a very good practice. Uninitialized variables have a default value of their type depending on the context in which they are used - booleans default to FALSE, integers and floats default to zero, strings (e.g. used in echo()) are set as an empty string and arrays become to an empty array.

But.. :

Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globals turned on. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array. isset() language construct can be used to detect if a variable has been already initialized.

Source: http://php.net/manual/en/language.variables.basics.php

0

精彩评论

暂无评论...
验证码 换一张
取 消