Php 从函数返回true或int的更好方法

Php 从函数返回true或int的更好方法,php,google-maps,Php,Google Maps,我有一个Address\u Finder类,它的方法是get\u Address($ordinal,$postcode),它混合使用谷歌地图API和数据库来获取完整地址 get_address从$ordinal和$postcode中查找地址详细信息:lat、lng、street_name等,调用后将实例的地址变量设置为结果 如果它成功找到地址,它会将内部数据设置为结果并返回true 但是,如果该地址已经存在于数据库中,它将返回数据库中该地址的地址\u id 显然,函数的返回值要么为true,要么

我有一个
Address\u Finder
类,它的方法是
get\u Address($ordinal,$postcode)
,它混合使用谷歌地图API和数据库来获取完整地址

get_address
$ordinal
$postcode
中查找地址详细信息:lat、lng、street_name等,调用后将实例的地址变量设置为结果

如果它成功找到地址,它会将内部数据设置为结果并返回
true

但是,如果该地址已经存在于数据库中,它将返回数据库中该地址的
地址\u id

显然,函数的返回值要么为true,要么为标准php整数类型

我们可以这样做:

$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
}
现在,
if($address\u id)
将在get\u address返回true或返回
$address\u id
1时计算true

这可以通过以下方式解决:

if ( $address_id === true ){
    // save returns the address ID of the last inserted address
    $address_id = $address -> save();
}
但是我想知道是否有更好的方法来做到这一点

像。。。我希望在我的博客上发布我的Address_Finder类作为API,以便其他程序员可以使用它


因此,通过“更好的方式”,我在思考程序员将如何使用我的API来返回数组而不是简单的值:

{
 'success':true||false,  //boolean indicating if get_address was successfull
 'id':     int||null     //id returned by get_address when successfull
}

您并没有告诉我们最重要的部分:调用方为什么以及如何关心数据库中是否已经存在地址以及其id是什么?是否有其他与
getAddress
相关的API方法?API设计不能在独立位中完成。如果两个不同的返回值混淆,请不要返回不同的值。:)抱歉,假设用户希望将地址id用作另一个数据库表中的外键。。。然后,他们将使用API从邮政编码中查找完整地址,并获取ID,而不管它是来自数据库还是谷歌API