You can use constructor injection:: (Fowler) 3 , : (interface injection), , ; (setter injection), (setter), ; (constructor injection), .
: (Fowler) 3 , : (interface injection), , ; (setter injection), (setter), ; (constructor injection), .
: (Fowler) 3 , : (interface injection), , ; (setter injection), (setter), ; (constructor injection), .
: (Fowler) 3 , : (interface injection), , ; (setter injection), (setter), ; (constructor injection), .
class Foo {
protected $dispatcher = null ;
public function __construct(sfEventDispatcher $dispatcher) {
$ this ->dispatcher = $dispatcher;
}
}
class Foo {
protected $dispatcher = null ;
public function setEventDispatcher(sfEventDispatcher $dispatcher) {
$ this ->dispatcher = $dispatcher;
}
}
Deciding which of these methods to use is a matter of taste. I prefer to use the constructor injection, since the objects are completely initialized at the creation stage. But when you have a large list of dependencies, using an installer deployment can be a better way, especially for optional dependencies.class Foo
{
// :
// ( ):
// static public function do_before($arr){
// echo "before!!!";
// }
public function send($foo, $bar)
{
// -
$ event = new sfEvent($ this , 'foo.do_before_send' , array( 'foo' => $foo, 'bar' => $bar));
// : :
// $this->dispatcher->connect('foo.do_before_send', 'Foo::do_before');
$ this ->dispatcher->notify($ event );
//
// $ret = ...;
// -
$ event = new sfEvent($ this , 'foo.do_after_send' , array( 'ret' => $ret));
$ this ->dispatcher->notify($ event );
return $ret;
}
}
class Foo
{
// ...
public function __call($method, $arguments)
{
// 'foo.method_is_not_found'
//
$ event = new sfEvent($ this , 'foo.method_is_not_found' , array( 'method' => $method, 'arguments' => $arguments));
// $method
$ this ->dispatcher->notifyUntil($ event );
// ?
if (!$ event ->isProcessed()) {
throw new sfException(sprintf( 'Call to undefined method %s::%s.' , get_class($ this ), $method));
}
// ,
return $ event ->getReturnValue();
}
}
class Bar
{
public function addBarMethodToFoo(sfEvent $ event )
{
// 'bar'
if ( 'bar' != $ event [ 'method' ])
{
//
return false ;
}
// ( foo)
$foo = $ event ->getSubject();
// bar
// , ,
// :
// $arguments = $event['arguments'];
$arguments = $ event [ 'parameters' ];
// -
// ...
//
$ event ->setReturnValue($someValue);
//
return true ;
}
}
$dispatcher->connect( 'foo.method_is_not_found' , array($bar, 'addBarMethodToFoo' ));
class Foo
{
// ...
public function render($template, $arguments = array())
{
//
$ event = new sfEvent($ this , 'foo.filter_arguments' );
// : :
// $this->dispatcher->connect('foo.filter_arguments', array(new Bar(), 'filterFooArguments'));
$ this ->dispatcher->filter($ event , $arguments);
//
$arguments = $ event ->getReturnValue();
// ...
// :
// return $arguments;
}
}
class Bar {
public function filterFooArguments(sfEvent $ event , $arguments) {
// :
// $arguments = array('33', '44');
$arguments[ 'processed' ] = true ;
return $arguments;
}
}
Source: https://habr.com/ru/post/90829/
All Articles