However, my experience is as follows: the following function tells us whether two users can communicate with each other. {Code...} Now, things have changed. In addition to telling whether to communicate or not, we also need to tell why, that is, if you want function calls to return false, you can tell the caller what is false. I used the following method... but my experience is as follows:
The following function shows whether two users can communicate with each other.
function a_can_talk_to_b($a, $b) { return (...) ? true : false;}
Now, things have changed. In addition to notifying you whether you can communicate with each otherWhy?That is, you want to tell the caller what is false when the function call returns false.
I used the following method: (pass a reference parameter why to the function and return it)
function a_can_talk_to_b($a, $b, &$why) { if (...) { return true; } else { $why = '...'; return false; }}
Another way is to return an array: (but the returned value is not a simple boolean value, which will change the function call method)
function a_can_talk_to_b($a, $b) { if (...) { $why = '...'; return array('status' => true, 'why' => $why); } else { $why = '...'; return array('status' => false, 'why' => $why); }}
Are there other methods besides these two methods?
Reply content:
However, my experiences are as follows:
The following function shows whether two users can communicate with each other.
function a_can_talk_to_b($a, $b) { return (...) ? true : false;}
Now, things have changed. In addition to notifying you whether you can communicate with each otherWhy?That is, you want to tell the caller what is false when the function call returns false.
I used the following method: (pass a reference parameter why to the function and return it)
function a_can_talk_to_b($a, $b, &$why) { if (...) { return true; } else { $why = '...'; return false; }}
Another way is to return an array: (but the returned value is not a simple boolean value, which will change the function call method)
function a_can_talk_to_b($a, $b) { if (...) { $why = '...'; return array('status' => true, 'why' => $why); } else { $why = '...'; return array('status' => false, 'why' => $why); }}
Are there other methods besides these two methods?
Well, there is no problem in terms of language.
Is this a good practice?
My suggestion is, it is best not to do this, because each time we have to upload another parameter that seems unrelated to the logic, it may be strange to use it.
If possible, throw an exception. You can use catch Exception to obtain the unexpected information.
This design requirement is unreasonable.
One function does one thing.
To do multiple tasks, separate functions.
To put it in a function, return an array at the beginning. No other moves.
return {'canTalk'=>true,'why'=>'balabala...'}
It is quite correct. If you use why elsewhere, it is also necessary to propose an encapsulation separately.
If you do not care about the convenience of returning true or false
CODE returned
Find the why according to the CODE => MESSAGE table.
The returned array and JSON object can solve your problem.