best practice for initializing class members in php

Posted by rgvcorley on Programmers See other posts from Programmers or by rgvcorley
Published on 2012-04-07T10:02:55Z Indexed on 2012/04/07 11:47 UTC
Read the original article Hit count: 213

I have lots of code like this in my constructors:-

function __construct($params) {

    $this->property = isset($params['property']) ? $params['property'] : default_val;

}

Is it better to do this rather than specify the default value in the property definition? i.e. public $property = default_val? Sometimes there is logic for the default value, and some default values are taken from other properties, which was why I was doing this in the constructor.

Should I be using setters so all the logic for default values is separated?

© Programmers or respective owner

Related posts about php

Related posts about object-oriented