I'm writing a web service client (in PHP). I think that the question below is applicable to APIs in general, but perhaps PHP has specific conventions for this.
I'm looking for a consistent naming convention to distinguish between functions that return the value of a member variable, and functions that perform web service requests to retrieve the remote record which corresponds to that member variable.
class User {
private $_address;
public function getAddress() {
return $this->_address;
}
public function pullAddress() {
$address = ... // Perform web service request to retrieve address
return $address;
}
}
*Note that I changed the example code to reference a member variable called address. The original code referenced a member variable called id. ID is a loaded term and distracting, so I changed it to a more neutral example.
*Perhaps some clarification on the nature of this web service client would help the discussion. This class is a client of a web service. I don't need to expose this class as a web service. When I'm using this class, I just want a convenient naming convention to distinguish between a function that performs a web service request, and a function that retrieves the value that was stored inside of the object.
The member would initially be populated via something like $u->setAddress(pullAddress()). Sometimes I just need to retrieve the current value stored in the object, which is when getAddress() comes into play.
getAddress() seems intuitive, but let me know if my usage above is not correct. pullAddress() seems a little clumsy. Perhaps requestAddress() would be better?
Stack Overflow will probably deem this question as not constructive, but any suggestions or examples would be appreciated.
Assuming you never want to pull an unset address without populating it, why not just test for being set before pulling from the DB and just use a single getter? You can even have a flag to 'force' a refresh of the value.
public function getAddress($forceRefresh = false) {
if( $forceRefresh == true || is_null($this->_address )) {
$this->_address = //... Perform the call to service to populate.
}
return $this->_address;
}