php - __callStatic 未被调用

标签 php magic-methods

不知道为什么,但它甚至没有击中我拥有的 var_dump() 。让我们看看我是如何实现它的。

<?php

namespace ImageUploader\Controllers;

class ApplicationController implements \Lib\Controller\BaseController {
    ....

    public function beforeAction($actionName = null, $actionArgs = null){}

    public function afterAction($actionName = null, $actionArgs = null){}

    public static function __callStatic($name, $args) {
        var_dump('hello?'); exit;
        if (method_exists($this, $name)) {
            $this->beforeAction($name, $args);
            $action = call_user_func(array($this, $name), $args);
            $this->afterAction($name, $args);
            return $action;
        }
    }
}

正如我们所看到的,我想在调用操作之前和之后执行一些操作,无论您是否实现了该方法。但 var_dump 永远无法到达。

此类扩展如下:

<?php

namespace ImageUploader\Controllers;

use \Freya\Factory\Pattern;

class DashboardController extends ApplicationController  {

    public function beforeAction($actionName = null, $actionArgs = null) {
        var_dump($actionName, $actionArgs); exit;
    }

    public static function indexAction($params = null) {
      Pattern::create('\Freya\Templates\Builder')->renderView(
          'dash/home',
          array(
              'flash' => new \Freya\Flash\Flash(),
              'template' => Pattern::create('\Freya\Templates\Builder')
          )
      );
    }

    ....

}

现在,当我这样做时: DashboardController::indexAction(); 它应该退出......除非我错过了一些东西。如果是这样的话 - 是什么?

即使是实现的 before_action(...) 中的 var_dump 也永远不会到达(显而易见,因为第一个,但如果我取出第一个第二个是永远达不到。)

最佳答案

__callStatic 仅当静态方法不存在时才被调用 - 由于 indexAction 实际上已定义,它的执行不会打扰 __callStatic()。 (Documentation)

实现您想要做的事情的方法可以是将 Controller 包装在装饰器中:

class ExtendedApplicationController
{
    /**
     * @var \Lib\Controller\BaseController
     */
    protected $controller;

    function __construct(\Lib\Controller\BaseController $controller) {
       $this->controller = $controller;
    }

    function __callStatic($name, $args) {
        if (method_exists($this->controller, 'beforeAction')) {
            call_user_func_array(array($this->controller, 'beforeAction'), $name, $args);
        }

        call_user_func_array(array($this->controller, $name), $args);

        if (method_exists($this->controller, 'afterAction')) {
            call_user_func_array(array($this->controller, 'afterAction'), $name, $args);
        }
    }
}

然后,在您的代码中,您可以执行以下操作:

$controller = new ExtendedApplicationController(new DashboardController());
$controller::indexAction();

我必须警告您,我在编写此方法时并未测试该方法,但我希望它能给您一个想法!

关于php - __callStatic 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30058572/

相关文章:

python - 为什么 functools.lru_cache 在处理普通方法时不缓存 __call__

php - 使用魔法方法有什么缺点?如何在 child 类(class)中缩写它们?

python - python 的所有内置函数都有神奇的方法吗?

python - 返回类型不同,重载__add__

python - `self[key] += value` 的魔术方法?

php - Magento index.php 301 将 http 重定向到 https

php - Google Analytics 在使用 Measurement 协议(protocol)时不注册交易

php - 如何在android应用程序和php服务器之间同步数据?

java - 我应该使用哪种 SOLR 实现方法; 3 个问题?

PHP 动态调整图像大小(即时)