简体   繁体   English

Symfony 2。 如果控制器的公共/私有/受保护以及有任何参数,如何确保控制器实现某种方法呢?

[英]Symfony2. How to ensure controller implements some method no matters if its public/private/protected and if it has any arguments?

I wonder if I can make controllers implementing some methods no matters if that method will be public , private or protected and if it will have any parameter. 我想知道是否可以使控制器实现某些方法,而无论该方法是publicprivate还是protected以及它是否具有任何参数。 I just want to ensure, that controller has method with specified name, no more. 我只想确保控制器没有指定名称的方法。

For example: 例如:

interface SomeInterface {
  function someFunction();
  function someOtherFunction();
}

class SomeController extends SomeBaseController implements SomeInterface {
  //some action
  public function someAction() { ... }

  //an implementation of SomeInterface method
  public function someFunction() { ... }  

  //an implementation of SomeInterface method
  protected function someOtherFunction($someParameter) { ... }
}

I know that it's not possible to do this with ordinary php interfaces but maybe there is some other way in php or maybe symfony2 has some tool to accomplish this? 我知道用普通的php接口是不可能的,但是php中可能还有其他方法,或者symfony2有一些工具可以做到这一点?

I know of one way to do this, which relies on the __call() method available in PHP: http://www.php.net/manual/en/language.oop5.overloading.php#object.call 我知道一种执行此操作的方法,该方法依赖于PHP中提供的__call()方法: http : //www.php.net/manual/en/language.oop5.overloading.php#object.call

__call() is triggered when invoking inaccessible methods in an object context. 

You could go ahead and create a parent class that looks like this - although it's not an Interface, so take care if you need it outside a single domain. 您可以继续创建一个看起来像这样的父类-尽管它不是一个Interface,但是请注意是否需要在单个域之外使用它。

class BaseClass{

    public function __call($name, $arguments) {
        $methodsToImplement = array(
            'method1', 'method2', 'method3'
        );

        if (in_array($name, $methodsToImplement)) {
            throw new Exception("Method " . $name . " is not yet implemented.");
        }
    }
}

It seems there is no way to accomplish this. 似乎没有办法实现这一目标。 So I mark my question as reslolved, however if someone knows an answer let me know! 因此,我将我的问题标记为已解决,但是如果有人知道答案,请告诉我! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道方法在PHP中是Public,Protected还是Private? - How to know if the method is Public, Protected or Private in PHP? 检查方法类型是公共,私有还是受保护的 - check method type if it is public, private or protected 适用于Symfony的FOSElasticaBundle2。 如何配置连接到集群? - FOSElasticaBundle for Symfony2. How to configure to connect to a cluster? 公共,私有或受保护的财产? - Public, Private or Protected properties? 调用受保护的方法Symfony \\ Bundle \\ FrameworkBundle \\ Controller \\ Controller :: generateUrl() - Call to protected method Symfony\Bundle\FrameworkBundle\Controller\Controller::generateUrl() 如何通过phpunit对具有多重内部调用保护/私有方法的方法进行单元测试? - How to unit test a method has multi inner call protected/private method by phpunit? 确保一个类至少实现一个方法 - Ensure a class implements at least one method Symfony:如何在Bundle Controller中调用私有服务 - Symfony: How to call private services in Bundle Controller 我应该如何测试对另一个受保护方法的公共方法调用? - How should I test a public method call to another protected method? 如何在静态方法中访问公共和私有变量? - How to access public and private variable with in the static method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM