When returning values in php, is it considered good or bad practice to return mixed data types. I'm working on a project where I am constantly faced with methods that return an id number or null
. I'm handling the null
value by checking for null
and returning -1
if it is null
.
在php中返回值时,返回混合数据类型是不错的做法。我正在开发一个项目,我经常遇到返回id号或null的方法。我通过检查null来处理null值,如果它为null则返回-1。
Another situation I find myself in a lot is where a method should do something and return a string. But sometimes it's not possible to return the string as it wasn't found or an exception happened. What's the best thing to do here? Return a string like 'failed' or something? This then creates a string coupling between methods, I think, as the calling method has to know exactly the string failure message to check for??
我发现自己的另一种情况是方法应该做什么并返回一个字符串。但有时候,由于找不到或发生异常,不可能返回字符串。这里最好的事情是什么?返回像'failed'之类的字符串?然后,这会在方法之间创建一个字符串耦合,因为调用方法必须准确知道要检查的字符串失败消息?
EDIT: OK there are a few different opinions already. I like the idea of returning false on failure and the actual result whatever its data type is on success. But... is there a defacto best practice when it comes to this? I mean, what do programmers in other languages do i.e. java and c++ etc in these situations?
编辑:好的,已经有一些不同的意见。我喜欢在失败时返回false的想法和实际结果,无论数据类型是成功的。但是......当谈到这个时,是否存在事实上的最佳实践?我的意思是,在这些情况下,其他语言的程序员会做什么,例如java和c ++等?
6 个解决方案
#1
8
What I usually do is if the method worked, return the value, and if it failed return FALSE. That's what a lot of PHP's built-in methods do. So, then you can just check if the function returned FALSE or not.
我通常做的是如果方法有效,返回值,如果失败则返回FALSE。这就是许多PHP的内置方法所做的事情。那么,你可以检查函数是否返回FALSE。
#2
4
I think it is bad practice to return mixed data types. It is possible, as you pointed out, but think about the readability and maintainability of your code. Make sure you comment what you are returning and why, I think that is going to be most important. If you are expecting back an int and you return -1 instead of null, comment that, so you (or someone else) doesn't go crazy trying to figure out what you were trying to do.
我认为返回混合数据类型是不好的做法。正如您所指出的那样,可以考虑代码的可读性和可维护性。确保你评论你要回归的内容和原因,我认为这将是最重要的。如果你期望返回一个int并且你返回-1而不是null,请注释,所以你(或其他人)不会疯狂试图找出你想要做的事情。
#3
4
Null is a fairly common return value to indicate that there is no return value. You should return null (not "failed", or -1) if the function wants to return no ID at all.
Null是一个相当常见的返回值,表示没有返回值。如果函数想要根本不返回任何ID,则应返回null(不是“失败”或-1)。
If it is exceptional that an ID was not found, you should throw an exception.
如果未找到ID,则应抛出异常。
#4
3
I agree with the answers above.
我同意上面的答案。
However if you design a whole system, the "best practice" would be to use exceptions: always return something meaningful, and in case of anomaly, throw an exception. The caller can then deal with the situations he knows how to face, and let somebody higher catch the rest.
但是,如果您设计整个系统,“最佳实践”将是使用异常:始终返回有意义的内容,并且在异常的情况下,抛出异常。然后呼叫者可以处理他知道如何面对的情况,并让其他人更好地抓住其余部分。
#5
3
Returning mixed type is bad, at least today in 2013. Boom! The way to go is to split this:
回归混合型是不好的,至少在今天是在2013年。轰!要走的路是分开这个:
BAD, mixed return type style:
坏,混合回归式风格:
function checkResult($data)
{
if ($data) {
...
return $stuff;
} else {
return false;
}
}
People will need additional logic to work checkRsult(), and they never know exactly what type will return.
人们将需要额外的逻辑来处理checkRsult(),他们永远不知道将返回什么类型。
GOOD, clearly fixed return type style:
好的,明确固定的返回式风格:
Maybe the example is not really good, but it shows the way to go.
也许这个例子不是很好,但它显示了要走的路。
function doesResultExist($data)
{
if ($data) {
return true;
}
// default return
return false;
}
function getResultData()
{
...
return $stuff;
}
#6
0
A function that returns mixed values is not considered bad., in fact thats the beauty of php, it being a dynamic language., so the thing to do is return false on failure and the required value if the function executes correctly,.
返回混合值的函数不被认为是坏的。实际上这就是php的美妙之处,它是一种动态语言。所以要做的就是在失败时返回false,如果函数正确执行则返回所需的值。
if( false == ( $data = do_something() ) ) return false;
else print_r( $data );
#1
8
What I usually do is if the method worked, return the value, and if it failed return FALSE. That's what a lot of PHP's built-in methods do. So, then you can just check if the function returned FALSE or not.
我通常做的是如果方法有效,返回值,如果失败则返回FALSE。这就是许多PHP的内置方法所做的事情。那么,你可以检查函数是否返回FALSE。
#2
4
I think it is bad practice to return mixed data types. It is possible, as you pointed out, but think about the readability and maintainability of your code. Make sure you comment what you are returning and why, I think that is going to be most important. If you are expecting back an int and you return -1 instead of null, comment that, so you (or someone else) doesn't go crazy trying to figure out what you were trying to do.
我认为返回混合数据类型是不好的做法。正如您所指出的那样,可以考虑代码的可读性和可维护性。确保你评论你要回归的内容和原因,我认为这将是最重要的。如果你期望返回一个int并且你返回-1而不是null,请注释,所以你(或其他人)不会疯狂试图找出你想要做的事情。
#3
4
Null is a fairly common return value to indicate that there is no return value. You should return null (not "failed", or -1) if the function wants to return no ID at all.
Null是一个相当常见的返回值,表示没有返回值。如果函数想要根本不返回任何ID,则应返回null(不是“失败”或-1)。
If it is exceptional that an ID was not found, you should throw an exception.
如果未找到ID,则应抛出异常。
#4
3
I agree with the answers above.
我同意上面的答案。
However if you design a whole system, the "best practice" would be to use exceptions: always return something meaningful, and in case of anomaly, throw an exception. The caller can then deal with the situations he knows how to face, and let somebody higher catch the rest.
但是,如果您设计整个系统,“最佳实践”将是使用异常:始终返回有意义的内容,并且在异常的情况下,抛出异常。然后呼叫者可以处理他知道如何面对的情况,并让其他人更好地抓住其余部分。
#5
3
Returning mixed type is bad, at least today in 2013. Boom! The way to go is to split this:
回归混合型是不好的,至少在今天是在2013年。轰!要走的路是分开这个:
BAD, mixed return type style:
坏,混合回归式风格:
function checkResult($data)
{
if ($data) {
...
return $stuff;
} else {
return false;
}
}
People will need additional logic to work checkRsult(), and they never know exactly what type will return.
人们将需要额外的逻辑来处理checkRsult(),他们永远不知道将返回什么类型。
GOOD, clearly fixed return type style:
好的,明确固定的返回式风格:
Maybe the example is not really good, but it shows the way to go.
也许这个例子不是很好,但它显示了要走的路。
function doesResultExist($data)
{
if ($data) {
return true;
}
// default return
return false;
}
function getResultData()
{
...
return $stuff;
}
#6
0
A function that returns mixed values is not considered bad., in fact thats the beauty of php, it being a dynamic language., so the thing to do is return false on failure and the required value if the function executes correctly,.
返回混合值的函数不被认为是坏的。实际上这就是php的美妙之处,它是一种动态语言。所以要做的就是在失败时返回false,如果函数正确执行则返回所需的值。
if( false == ( $data = do_something() ) ) return false;
else print_r( $data );