Created
January 21, 2014 19:20
-
-
Save mikegioia/8546422 to your computer and use it in GitHub Desktop.
Testing the DI overwriting and reset methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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