说我有类似的功能:
public function auth(){
return $someResponse;
}
public function collect(){
return $someOtherResponse
}
问题:当其中一个响应传递给另一个类时,有没有办法检查哪个函数返回了响应?
答案 0 :(得分:0)
根据我的评论,在回报中使用数组将为您提供可行的解决方案。
它将允许一种方式来查看已完成的工作;
function auth()
{
return (array("auth" => $someresponse));
}
function collect()
{
return (array("collect" => $someotherrpesonse));
}
class myClass
{
function doSomething($type)
{
if (function_exists($type))
{
$result = $type();
if (isset($result['auth']))
{
// Auth Used
$auth_result = $result['auth'];
}
else if (isset($result['collect']))
{
// Collect used
$collect_result = $result['collect'];
}
}
}
}
它还可以通过return array("fail" => "fail reason")
正如评论所说,你可以根据功能名称进行检查;
class myClass
{
function doSomething($type)
{
switch ($type)
{
case "auth" :
{
$result = auth();
break;
}
case "collect" :
{
$result = collect();
break;
}
default :
{
// Some error occurred?
}
}
}
}
无论哪种方式都有效且完全有效!
答案 1 :(得分:0)
以纯面向对象的方式,想要将信息附加到值类似于将其包装到具有上下文信息的容器中,例如:
class ValueWithContext {
private $value;
private $context;
public function __construct($value, $context) {
$this->value = $value;
$this->context = $context;
}
public value() {
return $this->value;
}
public context() {
return $this->context;
}
}
你可以像这样使用它:
function auth()
{
return new ValueWithContext($someresponse, "auth");
}
function collect()
{
return new ValueWithContext($someotherrpesonse, "collect");
}
这会强制您明确附加到值的上下文,这样可以保护您免受函数本身的意外重命名。
答案 2 :(得分:-1)
让两个用户定义的函数public byte[] compressByteArray(byte[] bytes){
ByteArrayOutputStream baos = null;
Deflater dfl = new Deflater();
dfl.setLevel(Deflater.BEST_COMPRESSION);
dfl.setInput(bytes);
dfl.finish();
baos = new ByteArrayOutputStream();
byte[] tmp = new byte[4*1024];
try{
while(!dfl.finished()){
int size = dfl.deflate(tmp);
baos.write(tmp, 0, size);
}
} catch (Exception ex){
} finally {
try{
if(baos != null) baos.close();
} catch(Exception ex){}
}
return baos.toByteArray();
}
& auth()
调用一个常用函数,调用collect()
函数应该可以解决问题。
debug_backtrace()
上述函数将输出哪个函数返回响应