我希望MyFilter
通过构造函数注入来设置一些属性,但这似乎是不可能的,Zend_View::addFilter(string $filter_class_name)
因为它会在使用时加载一个新实例。MyFilter
实现Zend_Filter_Interface
。
我可以以某种方式将过滤器的实例注入到 的实例中吗Zend_View
?
关闭,因为它(希望)将被推入 2.0,请参阅JIRA 上的票。
我希望MyFilter
通过构造函数注入来设置一些属性,但这似乎是不可能的,Zend_View::addFilter(string $filter_class_name)
因为它会在使用时加载一个新实例。MyFilter
实现Zend_Filter_Interface
。
我可以以某种方式将过滤器的实例注入到 的实例中吗Zend_View
?
关闭,因为它(希望)将被推入 2.0,请参阅JIRA 上的票。
你可以传递对象:
$filter = new Your_Filter($params); // implements Zend_Filter_Interface
$view->addFilter($filter);
您可以从 viewRenderer 获取视图实例,例如使用 staticHelper。
编辑:
另一种方法可能是:
class MyFilterSetup extends MyFilter implements Zend_Filter_Interface
{
public function __construct($params)
{
$this->_params = $params;
parent::__construct();
}
public function filter($string)
{
// .... $this->_params;
}
}
我不确定,但我认为这不可能。查看源代码setFilter()
,addFilter()
只接受过滤器类名作为字符串。您不能设置任何选项,例如可以设置Zend_Form
。你可以做的是:
class MyFilter implements Zend_Filter_Interface
{
protected static $_config;
public static setConfig(array $options)
{
self::_config = $options;
}
// ... do something with the options
}
然后你在需要的地方设置选项MyFilter::setOptions()
,所以当Zend_View
实例化过滤器实例时,它得到了正确运行过滤器所需的东西。
您不能在 1.x 分支中提交票证:
我们不能创建一个自定义视图对象扩展Zend_View
,覆盖addFilter()
方法以接受类或实例。然后重写该_filter()
方法来处理我们存储的两种类型的过滤器——字符串和实例。
为什么不将过滤器属性分配给视图,然后要么在设置视图时设置属性,要么直接在您的过滤函数中访问视图?例如
$view->assign('MyFilterProperty', 'fubar');
然后在您的过滤器类中:
public function setView($aView)
{
$this->_property = $aView->MyFilterPropery;
}
这很笨拙,但它应该可以完成工作。