Skip to content

Instantly share code, notes, and snippets.

@mikegioia
Created January 21, 2014 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikegioia/8546422 to your computer and use it in GitHub Desktop.
Save mikegioia/8546422 to your computer and use it in GitHub Desktop.
Testing the DI overwriting and reset methods
<?php
use Phalcon\DI as DI;
class Dummy {
private $test;
function getTest() {
return $this->test;
}
function setTest( $test ) {
$this->test = $test;
}
}
// set up default DI and dummy service
//
$di = new DI();
$di->setShared(
'dummy',
function() {
return new Dummy();
});
DI::setDefault( $di );
// first attempt at getting shared value
//
$dummy = DI::getDefault()->getShared( 'dummy' );
$dummy->setTest( 'something' );
echo "1: ", $dummy->getTest(), "\n";
// try to remove the service and check again
//
DI::getDefault()->remove( 'dummy' );
echo "2: ", DI::getDefault()->getShared( 'dummy' )->getTest(), "\n";
// try to overwrite the service
//
DI::getDefault()->setShared(
'dummy',
function() {
return new stdClass();
});
echo "3: ", DI::getDefault()->getShared( 'dummy' )->getTest(), "\n";
// finally, reset the default DI and try
//
DI::reset();
$di = new DI();
DI::setDefault( $di );
DI::getDefault()->setShared(
'dummy',
function() {
return new Dummy();
});
echo "4: ", DI::getDefault()->getShared( 'dummy' )->getTest(), "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment