I am trying to create an object that takes parameters into its constructor and then gives back an associative array. For example :
class RefArray {
public $ref_id,$title,$pub_date,$doi,$author_name;
public function __construct($ref_id, $title, $pub_date, $doi, $author_name){
$this = array('ref_id'=>$this->ref_id, 'title'=>$this->title,
'pub_date'=>$this->pub_date, 'doi'=> $this->doi,
'author_name'=>$this->author_name);
}
}
However, the code above gives this error: Fatal error: Cannot re-assign $this
The reason why I do this is to get around the restriction of not being able to have more than one constructor in PHP (the reference class takes an array in it's constructor).
class Reference {
private $ref_id, $title, $pub_date, $doi, $external_ref_id, $author_name;
public function __construct($refArray){
$this->setRefId($refArray["ref_id"]);
$this->setTitle($refArray["title"]);
$this->setPubDate($refArray["pub_date"]);
if(array_key_exists('doi', $refArray)){
$this->setDoi($refArray["doi"]);
}
$this->setExtRef();
if(array_key_exists('author_name', $refArray)){
$this->setAuthor($refArray["author_name"]);
}
}
So my question firstly is whether or not the idea of having a class to make an associative array is a good one. Secondly if it is how do I make it work?