4Answers
  • 15
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Initializing properties in PHP

While I'm led to believe its likely just a design choice, is there any advantage 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)

php

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)

  • 11
Reply Report
      • 2
    • @Tomcat: if I always should override this array I prefer to omit initialization. But if empty array is correct default value for me, I initialize it(ex. class Validator{ private $error=array();//functions that can push something to $errors} )
      • 2
    • Thanks @RiaD - This is why I had initially started initializing values to null, or more importantly array(); however, I've begun using PHPDoc comments thoroughly in my projects which (save for the circumstances where a value is type-checked before use) makes it largely pointless. I like your initialization choices; I'll put that into practice.
      • 2
    • anyway I think it's better to check assigment value, than read that read something like "defaults to null". I understand that you anyway will read it but my subconscious understand = null faster
      • 1
    • Regarding your point to avoid initializing properties to array(); what is the disadvantage? You mention you cannot use some functions, however I would think its more prohibitive to omit an array() initialization if you're going to be performing operations on the property before a value has been applied.

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.

  • 3
Reply Report
      • 2
    • Thanks @OZ_ - That's the circumstance I neglected to mention but was aware of regarding arrays. However if through some other method of initialization, or during instantiation, the property intended to be an array is assigned an array value (unconditionally), it makes at that point no difference.

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
Reply Report
      • 2
    • Thanks @Quasdunk - This is why I initially put this into practice, and ease of understanding when perusing code I haven't looked at in awhile. However, as I mentioned in another comment, I've begun using PHPDoc comments everywhere, er go, the readability factor is no longer important.
      • 1
    • @Tomcat: and reasons, used in this qoute are actuulay only for usual variables, You get not E_NOTICE, you can't override in other files or by register_globals

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions