Skip to content

Instantly share code, notes, and snippets.

@touhonoob
Forked from mikegioia/phalcon_di_reset.php
Last active August 29, 2015 14:06
Show Gist options
  • Save touhonoob/a0391c9b3c2680815d9a to your computer and use it in GitHub Desktop.
Save touhonoob/a0391c9b3c2680815d9a to your computer and use it in GitHub Desktop.
<?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