Hi,
I'd like some suggestions/critique/opinions on the following setup of class "bar".
Both classes "bar" and "foo" hold the same data and do the same stuff, the difference being that in one case data is held in an array whereas in the other there are distinct variables.
Is there anything fundamentally wrong with that approach that would make you say "I would never do that"?
Obviously it's not easily possible to convert the variables in the array to "public" - but I do not intend to do so - I prefer using accessors and mutators for exposing class variables.
The advantage that I see is that I can build a "lazy" constructor where I can simply fill the whole class-data in one swoop - for example from an array that I got through a datareader class that retrieves them for me from a DB.
class bar
{
private $_data = array (
'myVar1'=>'',
'myVar2'=>0,
'myVar3'=>null
);
public function __construct($array)
{
$this->_data = $array;
}
/**
* mutators like this would provide public access to variables
* @param $string
* @return void
*/
public function setMyVar1($string)
{
$this->_data['myVar1'] = $string;
}
/**
* accessors like this would provide public access to variables
*
* @return $string
*/
public function getMyVar1()
{
return $this->_data['myVar1'];
}
/**
* some function that does something
* @return void
*/
public function doSomethingUseful()
{
}
}
class foo
{
private $_myVar1 = '';
private $_myVar2 = 0;
private $_myVar3 = null;
public function __construct($string, $int, $obj)
{
$this->_myVar1 = $string;
$this->_myVar2 = $int;
$this->_myVar3 = $obj;
}
/**
* mutators like this would provide public access to variables
* @param $string
* @return void
*/
public function setMyVar1($string)
{
$this->_myVar1 = $string;
}
/**
* accessors like this would provide public access to variables
*
* @return $string
*/
public function getMyVar1()
{
return $this->_myVar1;
}
/**
* some function that does something
* @return void
*/
public function doSomethingUseful()
{
}
}