I have a Address_Finder class with a method get_address( $ordinal, $postcode ) that uses a mixture of google map API and a database to get a full address
get_address finds the address details: lat, lng, street_name, etc from a $ordinal and $postcode and once called, it sets the instance’s address variables to the results.
If it finds the address successfully, it sets the internal data to the results and returns true.
However if the address already exists in the database, it returns the address_id of the address in the database.
So obviously the return values for the function are either true, or a standard php integer type.
We could do something like this:
$address = new Address_Finder();
$address_id = $address -> get_address( "2", "NG8 5NW" );
if ( $address_id ){
$address_id = $address -> save(); // saves the address to the database
}
Now the if ( $address_id ) will evaluate true if get_address returns true, or if it returns the $address_id 1.
This could be solved by doing:
if ( $address_id === true ){
// save returns the address ID of the last inserted address
$address_id = $address -> save();
}
However I was wondering if there way a better way to do this?
Like… I was hoping to release my Address_Finder class as an API on my blog so other programmers out there can use it.
So by ‘better way’, I’m thinking in terms of how programmers will use my API
Return an array instead of a simple value: