Skip to content

Instantly share code, notes, and snippets.

@SocalNick
Created March 19, 2012 16:38
Show Gist options
  • Save SocalNick/2118520 to your computer and use it in GitHub Desktop.
Save SocalNick/2118520 to your computer and use it in GitHub Desktop.
Can't declare injection method in base class
<?php
namespace Application {
abstract class Page {
public $blocks;
public function addBlock(Block $block){
$this->blocks[] = $block;
}
}
class FancyPage extends Page{
}
class BoringPage extends Page{
}
interface Block {
}
}
namespace DifferentNs {
class BlockOne implements \Application\Block {}
class BlockTwo implements \Application\Block {}
}
namespace {
// bootstrap
include 'zf2bootstrap' . ((stream_resolve_include_path('zf2bootstrap.php')) ? '.php' : '.dist.php');
$di = new Zend\Di\Di;
$di->configure(new Zend\Di\Configuration(array(
'definition' => array(
'class' => array(
'Application\Page' => array(
'addBlock' => array(
'block' => array(
'type' => 'Application\Block',
'required' => true
),
),
),
),
),
'instance' => array(
'Application\FancyPage' => array(
'injections' => array(
'DifferentNs\BlockOne',
'DifferentNs\BlockTwo'
),
),
'Application\BoringPage' =>array(
'injections' => array(
'DifferentNs\BlockOne',
),
),
)
)));
$fancyPage = $di->get('Application\FancyPage');
$boringPage = $di->get('Application\BoringPage');
// expression to test
$works = (
$fancyPage->blocks[0] instanceof DifferentNs\BlockOne
&& $fancyPage->blocks[1] instanceof DifferentNs\BlockTwo
&& $boringPage->blocks[0] instanceof DifferentNs\BlockOne
&& !isset($boringPage->blocks[1])
);
// display result
echo (($works) ? 'It works!' : 'It DOES NOT work!') . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment