【问题标题】:How to force a method to be called如何强制调用方法
【发布时间】:2014-10-03 20:13:48
【问题描述】:

我正在创建一个 API,我有一个名为 Service 的类,它有一个函数 requireType()

$typeGETPUTPOSTDELETE 之一

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


【解决方案1】:

我会将构造函数放在Service 类中:

class Service {
  final public function __construct($type) {
    $this->requireType($type);
  }
}

这样,任何从服务扩展的类都必须使用$type 构造并调用函数

或者,如果每次调用 myAction() 具有不同的类型很重要,您可以在 Service 类中拥有一个抽象方法和一个定义方法:

abstract class Service {
  final public function myAction($type) {
    $this->requireType($type);
    $this->customAction();
  }
  protected function customAction();
}

这样,每个人都用他们想要的类型(由 php 强制)调用myAction(),然后myAction() 调用必须在扩展类中定义的customAction()

【讨论】:

  • $type 因每种方法而异。
  • 这就是为什么你将它作为参数传递给类扩展服务。
  • 这不起作用MethodA 可能需要POSTMethodB 可能需要GET,如果你通过构造函数传递它,你就不能为每个方法创建它。
  • 这有点设计问题的味道......如果他们必须在使用任何函数之前调用 requireType ,您很可能应该重新考虑您的类?你在使用接口吗?抽象类?为什么调用 requireType 是强制性的,但还不清楚他们必须管理它?
  • 好吧,也许这会让它更清楚:客户端example.comapi.example2.com 发送PUT 请求api 知道客户端发送的方法需要说什么,如果你要打电话给我,您需要发送GET 请求,而不是PUT 请求。再试一次。
【解决方案2】:

在基本控制器构造函数中,将名为“callRequireType”的实例变量设置为 false。

在 requireType 中,将 calledRequireType 设置为 true。

在...任何你想检查的代码是否调用它(大概是调用 xxAction 的同一个处理程序,在调用 xxAction 之后),检查 calledRequireType 并抛出异常。

【讨论】:

  • 他们将编写进行检查的函数,而不是操作 :)
  • 在这种情况下,将 calledRequireType 放在它们扩展的基类中。我会更新我的答案
猜你喜欢
  • 2021-06-09
  • 2010-11-19
  • 2011-06-18
  • 2011-04-28
  • 1970-01-01
  • 1970-01-01
  • 2020-12-03
  • 2010-10-21
  • 1970-01-01
相关资源
最近更新 更多