Zend Framework 2
Zend\Di Debugging & Complex Use Cases
Debugging a DiC
It is possible to dump the information contained within both the Definition and InstanceManager for a Di instance.
The easiest way is to do the following:
Zend\Di\Display\Console :: export ( $di );
If you are using a RuntimeDefinition where upon you expect a particular definition to be resolve at the first-call,
you can see that information to the console display to force it to read that class:
Zend\Di\Display\Console :: export ( $di , array ( 'A\ClassIWantTo\GetTheDefinitionFor' ));
Complex Use Cases
Interface Injection
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 namespace Foo\Bar {
class Baz implements BamAwareInterface
{
public $bam ;
public function setBam ( Bam $bam )
{
$this -> bam = $bam ;
}
}
class Bam
{
}
interface BamAwareInterface
{
public function setBam ( Bam $bam );
}
}
namespace {
include 'zf2bootstrap.php' ;
$di = new Zend\Di\Di ;
$baz = $di -> get ( 'Foo\Bar\Baz' );
}
Setter Injection with Class Definition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 namespace Foo\Bar {
class Baz
{
public $bam ;
public function setBam ( Bam $bam )
{
$this -> bam = $bam ;
}
}
class Bam {
}
}
namespace {
$di = new Zend\Di\Di ;
$di -> configure ( new Zend\Di\Config ( array (
'definition' => array (
'class' => array (
'Foo\Bar\Baz' => array (
'setBam' => array ( 'required' => true )
)
)
)
)));
$baz = $di -> get ( 'Foo\Bar\Baz' );
}
Multiple Injections To A Single Injection Point
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 namespace Application {
class Page
{
public $blocks ;
public function addBlock ( Block $block )
{
$this -> blocks [] = $block ;
}
}
interface Block
{
}
}
namespace MyModule {
class BlockOne implements \Application\Block {}
class BlockTwo implements \Application\Block {}
}
namespace {
include 'zf2bootstrap.php' ;
$di = new Zend\Di\Di ;
$di -> configure ( new Zend\Di\Config ( array (
'definition' => array (
'class' => array (
'Application\Page' => array (
'addBlock' => array (
'block' => array ( 'type' => 'Application\Block' , 'required' => true )
)
)
)
),
'instance' => array (
'Application\Page' => array (
'injections' => array (
'MyModule\BlockOne' ,
'MyModule\BlockTwo'
)
)
)
)));
$page = $di -> get ( 'Application\Page' );
}
Edit this document
The source code of this file is hosted on GitHub. Everyone can
update and fix errors in this document with few clicks -
no downloads needed.
Login with your GitHub account.
Go to
Zend\Di Debugging & Complex Use Cases
on GitHub.
Edit file contents using GitHub's text editor in your web browser
Fill in the Commit message text box at the end of the page telling why
you did the changes. Press Propose file change button next to it when done.
On Send a pull request page you don't need to fill in text anymore. Just
press Send pull request button.
Your changes are now queued for review under project's Pull requests tab on GitHub.