__callStatic 未被调用
__callStatic not being called
不知道为什么,但它甚至没有达到我拥有的 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
从未达到。
此 class 扩展于:
<?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)
实现您正在尝试做的事情的一种方法是将您的控制器包装在装饰器中:
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();
我必须警告你,我在编写它时没有测试这种方法,但我希望它能给你一个想法!
不知道为什么,但它甚至没有达到我拥有的 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
从未达到。
此 class 扩展于:
<?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)
实现您正在尝试做的事情的一种方法是将您的控制器包装在装饰器中:
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();
我必须警告你,我在编写它时没有测试这种方法,但我希望它能给你一个想法!