I am fairly new to PHP and OOP. Having said that, I wanted to know how independent functions should be within a class. I know that each function should be responsible for doing just one thing. However, as you can see in my class below, I occasionally find myself creating functions that build upon one another in a "cascade" type fashion.
Is this good practice? Is there a different way I should be structuring this class/these types of functions? I want to make sure I am developing good habits.
Thanks in advance for your help.
class PubMedQuery {
private $query;
private $searchParameters;
private $searchURL;
private $fetchParameters;
private $fetchURL;
private $searchResults;
private $fetchResults;
private $matches;
private $matchRegex;
private $emailAddresses;
public function __construct($query) {
$this->query = $query;
}
public function setSearchParameters() {
$this->searchParameters = array(
'db' => 'pubmed',
'term' => $this->query,
'retmode' => 'xml',
'retstart' => '0',
'retmax' => '1000',
'usehistory' => 'y'
);
}
public function getSearchParameters() {
return $this->searchParameters;
}
public function setFetchParameters() {
$this->fetchParameters = array(
'db' => 'pubmed',
'retmax' => '1000',
'query_key' => (string) $this->searchResults->QueryKey,
'WebEnv' => (string) $this->searchResults->WebEnv
);
}
public function getFetchParameters() {
return $this->fetchParameters;
}
public function setSearchURL() {
$this->baseSearchURL = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils /esearch.fcgi?';
$this->searchURL = $this->baseSearchURL . http_build_query($this->getSearchParameters());
}
public function getSearchURL() {
return $this->searchURL;
}
public function setFetchURL() {
$this->baseFetchURL = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?';
$this->fetchURL = $this->baseFetchURL . http_build_query($this->getFetchParameters());
}
public function getFetchURL() {
return $this->fetchURL;
}
public function setSearchResults() {
$this->setSearchParameters();
$this->setSearchURL();
$this->searchResults = simplexml_load_file($this->getSearchURL());
}
public function getSearchResults() {
$this->setFetchParameters();
$this->setFetchURL();
return file_get_contents($this->getFetchURL());
}
public function setEmailAddresses() {
$this->matches = array();
$this->matchRegex = '/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/';
preg_match_all($this->matchRegex, $this->getSearchResults(), $this->matches);
$this->emailAddresses = array_unique(array_values($this->matches[0]));
}
public function getEmailAddresses() {
$this->setSearchResults();
$this->getSearchResults();
$this->setEmailAddresses();
return $this->emailAddresses;
}
}
//Example using search term "psoriasis"
$query = new PubMedQuery('psoriasis');
echo implode('<br />', $query->getEmailAddresses());
public $baseSearchURL = 'http://url.com';
Or you could set them all in the__construct()
function. That removes the need for a setter and a getter for that variable. Also, every time you runsetSearchURL()
you're also setting the$baseSearchURL
variable, even though its the same string. If you take this out then it means you only set it once. Make your code more readable - but don't go OTT.getSearchResults()
runs the functiongetFetchURL()
which just sets variables. This is a bit OTT and actually makes the code less readable and maintainable (again IMO) because I'm having to constantly scroll through your code to work out what you're doing and what's happening on EVERY line.