【发布时间】:2014-10-03 20:13:48
【问题描述】:
我正在创建一个 API,我有一个名为 Service 的类,它有一个函数 requireType()
$type 是 GET、PUT、POST 或 DELETE 之一
final public function requireType($type){
// Do some stuff
}
我还有一个名为API 的主类,它创建了一个从API 调用的类的实例,我们称之为ClassA,然后扩展Service。
class ClassA extends Service{
// Writer called "$this->requireType()" method is fine to run
public function myAction(){
// Make sure $this->requireType() was called here before anything else gets run
$this->requireType("POST");
// Do some stuff
}
// Writer did not call "$this->requireType()" method
// An error should occur
public function myAction(){
// Do some stuff
}
}
我想知道的是,在我的API 课程中,我如何(如果可能)检查requireType() 是否立即在myAction() 中被调用?
API 类方法调用看起来像这样(简化):
// This method creates an instance of "ClassA" and runs "myAction()"
// We should check to see if the person who wrote the method "myAction()"
// called "$this->requireType()" or not. If not throw an Error
final public function execServiceRequest($clientData = ""){
$class = $this->getService();
$reflection = new ReflectionMethod($class, $this->action);
$obj = new $class();
$obj->setReadWrite($this->read, $this->write);
$obj->initPage($this->config);
$obj->setData($clientData);
$obj->before();
$response = call_user_func(array($obj, $this->action));
$obj->after();
}
所以最后我想要这样的东西:
来自example.com 的客户端向api.example2.com 发送PUT 请求,api.example2com 知道客户端发送了什么。被调用的方法不知道,它需要说明如果你要打电话给我,你需要发送一个GET 请求,如果你不这样做,你需要发送一个带有GET 标头的不同请求。访问此方法。
我主要想添加此功能,以便创建新方法的开发人员不会编写会意外接受 4 种标头类型中的任何一种的代码。
【问题讨论】:
-
嗯... th.. 什么?你能在你想知道
requireType()是否被调用的那一行添加评论吗? -
基本上我只是想确保在扩展
Service的类中编写新公共方法的人通过调用$this->requireType()来启动该方法
标签: php