在Zend中使用Reflection时如何忽略/跳过函数和方法

时间:2011-08-17 10:42:58

标签: php zend-framework reflection phpdoc

我正在使用PHP 5.2.6使用Zend_JSON_ServerZend_XmlRpc_Server构建多协议Web服务。我现在面临的问题是服务器现在宣布我的班级及其父母的每一个方法。

class MyClass extends MyInterface {
  /**
   * To be published and accessible
   **/
  public function externalUse() {}
}

class MyInterface {
  /**
   * This method should not be published using the webservice,
   * but needs to be of type "public" for internal use
   * @access private
   * @ignore
   **/
   public function internalUseOnly() {}
}

$server = new Zend_Json_Server();
$server->setClass('MyClass');

// show service map
header('Content-Type: text/plain');
$server->setTarget('/service.json')
       ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);

$smd = $server->getServiceMap();
die($smd);

此代码还将使用webservice宣布internalUseOnly为可访问函数:

{ "SMDVersion" : "2.0",
  "contentType" : "application/json",
  "envelope" : "JSON-RPC-2.0",
  "methods" : { "externalUse" : { "envelope" : "JSON-RPC-2.0",
          "parameters" : [  ],
          "returns" : "null",
          "transport" : "POST"
        },
      "internalUseOnly" : { "envelope" : "JSON-RPC-2.0",
          "parameters" : [  ],
          "returns" : "null",
          "transport" : "POST"
        }
    },
  "services" : { "externalUse" : { "envelope" : "JSON-RPC-2.0",
          "parameters" : [  ],
          "returns" : "null",
          "transport" : "POST"
        },
      "internalUseOnly" : { "envelope" : "JSON-RPC-2.0",
          "parameters" : [  ],
          "returns" : "null",
          "transport" : "POST"
        }
    },
  "target" : "/service.json",
  "transport" : "POST"
}

正如您所看到的,我已经尝试了@ignore@access private,但两者都被Reflection忽略了。还有其他选项可以从我的服务地图中删除这些功能吗?

提前致谢!


更新

我发现的一个解决方案是使用重载和动态函数调用再次访问私有/受保护的方法。但由于call_user_func并不是一种禁食方法,因此这只是一个修补程序。

class MyInterface {
    public function __call($method, $params) {
        $method = '_' . $method;
        if (method_exists($this, $method)) {
            call_user_func(array($this, $method), $params);
        }
    }

  /**
   * This method should not be published using the webservice,
   * but needs to be of type "public" for internal use
   * @ignore
   * @access private
   * @internal
   **/
  protected function _internalUseOnly() { die('internal use only!'); }
}

1 个答案:

答案 0 :(得分:0)

使用__前缀您的方法。 Zend \ Server \ Reflection \ ReflectionClass忽略以__开头的方法。至少在ZF2中。