2011-08-17 53 views
1

我正在使用PHP 5.2.6構建使用Zend_JSON_ServerZend_XmlRpc_Server的多協議web服務。而我目前面臨的問題是,服務器現在正在宣佈我的課程及其父母的每一種方法。如何在Zend中使用Reflection時忽略/跳過函數和方法

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); 

此代碼也將公佈internalUseOnly爲使用WebService的訪問功能:

{ "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,但兩者都通過反射忽略。有沒有其他選項可以從我的服務地圖中刪除這些功能?

在此先感謝!


更新

一個解決方案,我發現是讓私營/保護方法可以訪問再次使用重載和動態函數調用。但是因爲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!'); } 
} 
+0

@internal怎麼樣? – Pelshoff 2011-08-17 10:46:08

回答

0

用__前綴你的方法。 Zend \ Server \ Reflection \ ReflectionClass忽略以__開頭的方法。至少在ZF2中。

相關問題