Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.